青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 297,  comments - 15,  trackbacks - 0

c++ class template header and implementation (skeleton) definitions are often hard to read, let alone to write. Especially when defining template classes that are derived from base template classes, I often find myself struggling for the correct syntax.

In this article, I will present a template-based source code generator to produce C++ class template header implementation (skeleton) definitions in .hpp and .cpp files, based on a minimal yet functional set of methods.

A template is a way to specify generic code, with a placeholder for the type. Note that the type is the only "parameter" of a template, but a very powerful one, since anything from a function to a class (or a routine) can be specified in "general" terms without concerning yourself about the specific type. Yet. These details are postponed until you start to use the template. You can consider templates to be compile-time polymorphic, yet typesafe (in contrast to C MACROs).

Function vs. Class
When talking about C++ templates, one should realize that there are, in fact, two kinds of templates: function templates and class templates. The former are quite easy to implement, because they usually only contain the template(s) in their definition. As an example of a function template, here is a function that produces the minimum of two arguments, without specifying the actual type of the arguments:

template <typename T>
T max(const T &X, const T &Y)
{
if (X > Y)
return X;
else
return Y;
}
T is the usual template character that is used to specify the typename, which—at the time of definition—is unknown, and will be determined when you actually use the template in your source code. Here is an example:
int x = max(6, 42); // compiler determines T = int
float y = max(3.1415927, 2.20371); // compiler determines T = float
Or explicitly, as follows:
int x = max<int> (6, 42); // explicit template syntax
The C++ compiler will be able to determine—at compile time—where the calls to this function template are made, which argument types are used, and hence which "expansions" of this function template have to be generated (like a MACRO expansion) and then compiled and linked into an executable. All this is happening behind the scenes, of course, although template expansion can take a lot of compiler and linker resource (as you may find out when you start to use them more often).

Class templates are similar to function templates in that the compiler will determine at compile-time which expansions (or instantions) of the class template are needed. The fact that they are classes and not merely functions, makes the syntax a bit more difficult, however.

Pop Quiz
Even if you're an experienced C++ class template user, could you tell me from the top of your head what the syntax would be of the implementation skeleton for the copy constructor of a template class TDerived, which is derived from a template class TBase? You have 10 seconds ...

It turns out to be as follows:

template <class T> TDerived<T>::TDerived(const TDerived<T>& copy): TBase<T>(copy)
But I don't blame you if you couldn't come up with that right away. If you did know the answer, then you probably don't need to read the remainder of this article, unless you're also interested in a template-based template header/source generator. That's what I made for myself, to help me remember.

Canonical Class
But before I want to continue with class templates, let's first talk about a minimum useful class, sometimes also called a canonical class. By this I mean a class definition which is minimal (only a few key methods), but still complete and useful. This means that the class should at least contain the default constructor (without an argument), the destructor, a copy constructor, the assignment operator, the compare operator and last—optionally—the stream operator (always useful when debugging). When a class contains at least these methods, we can call it a canonical class.

Since I'm planning to produce a template header and source generator, it's important to realise what I should generate and what not. Making sure that I produce a canonical class—especially when it comes to templates, can make the difference between a nice but useless or an actual useful tool. As an example, here is the canonical class definition (header file) of a class TBase:

class TBase
{
public:
// Constructors & Destructors
TBase(void);
TBase(const TBase& copy);
virtual ~TBase(void);

// Operator overloading
TBase& operator = (const TBase& other);
int operator == (const TBase& other) const;

// Output
friend ostream& operator << (ostream& os, const TBase& other);
};

Canonical Class Template
We can modify the listing above to turn it into a canonical template class definition. Just like function templates, this means we have to use the <T> template syntax in a few places, and sometimes in more than a few. Luckily, it's not that hard, and the result can be seen in the following listing:
template <class T> class TBase
{
public:
// Constructors & Destructors
TBase(void);
TBase(const TBase<T>& copy);
virtual ~TBase(void);

// Operator overloading
TBase<T>& operator = (const TBase<T>& other);
int operator == (const TBase<T>& other) const;

// Output
friend ostream& operator << (ostream& os, const TBase<T>& other);
};

Just to let you know what the implementation looks like (the empty
skeletons, that is), take a look at the following listing:
// Constructors & Destructors
template <class T> TBase<T>::TBase(void) {}
template <class T> TBase<T>::TBase(const TBase<T>& copy) {}
template <class T> TBase<T>::~TBase(void) {}

// Operator overloading
template <class T> TBase<T>& TBase<T>::operator = (const TBase<T>& other) {}
template <class T> int TBase<T>::operator == (const TBase<T>& other) const {}

// Output
template <class T> ostream& operator << (ostream& os, const TBase<T>& other) {}

This is usually the place where I could do with a little help or support
to get the class template syntax right.

Derived Templates
If you've been able to keep up with me so far, then let's get to the final round: templates derived from other templates. Sometimes you just have to derive your own custom class template TDerived from a base template class TBase (sound familiar?). And just for your amusement (and mine), I've included the header listing for the derived canonical class template definition below:

template <class T> class TDerived: public TBase<T> { public: // Constructors & Destructors TDerived(void); TDerived(const TDerived<T>& copy); virtual ~TDerived(void); // Operator overloading TDerived<T>& operator = (const TDerived<T>& other); int operator == (const TDerived<T>& other) const; // Output friend ostream& operator << (ostream& os, const TDerived<T>& other); }; Certainly this TDerived class template definition needs a list of empty implementation skeletons, which are defined as follows (empty because they're skeletons, but they still need to be implemented by the programmer, of course). // Constructors & Destructors template <class T> TDerived<T>::TDerived(void): TBase<T>() {} template <class T> TDerived<T>::TDerived(const TDerived<T>& copy): TBase<T>(copy) {} template <class T> TDerived<T>::~TDerived(void) {} // Operator overloading template <class T> TDerived<T>& TDerived<T>::operator = (const TDerived<T>& other) {} template <class T> int TDerived<T>::operator == (const TDerived<T>& other) const {} // Output template <class T> ostream& operator << (ostream& os, const TDerived<T>& other) {} OK, who could already produce the above listing without a second thought? If you could, then you probably didn't need to read this article, because the fun stuff is over. What remains is the description of a little tool that I made for myself to actually produce and generate the output listings that we've seen so far.
Template Template
If you look closely at the listings presented so far, you can see a pattern (believe me, there is logic behind this class template syntax). In fact, I have been able to produce two template files that can be used to generate the template listings we've seen in this article. The template file for the class definition (typically inside a header file) is defined as follows:
//    File: <#class>.hpp
// Author: drs. Robert E. Swart>
// Date: <#date>
// Time: <#time>
// Version: 0.01
// Generated by: HeadGen (c) 1995-2001 by Bob Swart
(aka Dr.Bob - www.drbob42.com)
// Changes:
//

#ifndef <#class>_hpp
#define <#class>_hpp

#include <iostream.h>
<#includebase>

template <class <#templatechar>> class <#class> <#publicbase>
{
public:
// Constructors & Destructors
<#class>(void);
<#class>(const <#class><#template>& copy);
virtual ~<#class>(void);>

// Accessing functions

// Modifier functions

// Operator overloading
<#class><#template>& operator = (const <#class><#template>& other);
int operator == (const <#class><#template>& other) const;

// Streaming output
friend ostream& operator << (ostream& os, const <#class><#template>& other);

protected:
private:
};

#endif

Note the special #-tags. WebBroker developers may recognize these as
tags used in the PageProducer components. That's actually the case,
since I'm using a TPageProducer component (from the Internet tab) to
expand the above template into a true class template definition
header—with or without a template base class.
The same technique can be applied to the following template listing,
that can be used to produce the empty template skeleton implementations:
// File: <#class>.cpp
// Author: drs. Robert E. Swart
// Date: <#date>
// Time: <#time>
// Version: 0.01
// Generated by: HeadGen (c) 1995-2001 by Bob Swart
(aka Dr.Bob - www.drbob42.com)
// Changes:
//

#include "<#include>.hpp"

// Constructors & Destructors
template <class <#templatechar>> <#class><#template>::<#class>(void) <#base>
<#body>

template <class <#templatechar>> <#class><#template>::<#class>(const
<#class><#template>& copy) <#basecopy>
<#body>

template <class <#templatechar>> <#class><#template>::~<#class>(void)
<#body>

// Operator overloading
template <class <#templatechar>> <#class><#template>& <#class><#template>::operator = (const <
#class><#template>& other)
<#body>

template <class <#templatechar>> int <#class><#template>::operator == (const
<#class><#template>& other) const
<#body>

// Streaming output
template <class <#templatechar>> ostream& operator << (ostream&
os, const <#class><#template>& other)
<#body>

Again, the above listing can be used to produce a stand-alone class
template as well as a derived class template. We only need to specify
three options: the class name, the (optional) base class name, and the
template character.

HeadGen

 The utility HeadGen only requires the class name (the template character is T by default), as can be seen in Figure 1.

For the base class, specify Base in the Class Name box leave the Ancestor type box empty, and click on Generate. For the derived class, specify Derived in the Class Name box, Base in the Ancestor Type box and then click on Generate again. In both cases, the T will be added as prefix automatically (files Base.hpp and Base.cpp will contain the definition for TBase).

A very simple Borland C++Builder example program (to test the syntax of the generated files) can be seen below:

//--------------------------------------------------------- #pragma hdrstop #include "Base.cpp" // TBase #include "Derived.cpp"; // TDerived //-------------------------------------------------------- typedef TDerived<int> TintClass; #pragma argsused int main(int argc, char* argv[]) { TintClass* Bob = new TintClass(); TintClass Swart = TintClass(*Bob); if (*Bob == Swart) { *Bob = Swart; } return 0; } //-------------------------------------------------------- Note that I needed to include the .cpp files of the templates, and not only (or just) the .hpp files. That's because the .cpp files are "expanded" (like MACROs) to the compiler, which must find them in order to be able to use them. External Templates
The two template files are external files HeadGen.h (for the .hpp header) and HeadGen.c (for the .cpp source file). As an additional benefit, you can edit these templates and make sure your own copyright statements appear in them. Make sure to keep all #-tags intact, though, otherwise the template PageProducer won't be able to work correctly anymore.



from:
http://www.devx.com/cplus/Article/20689/0/page/1

posted on 2010-04-19 11:05 chatler 閱讀(640) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Template

只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺(jué)這個(gè)博客還是不錯(cuò),雖然做的東西和我不大相關(guān),覺(jué)得看看還是有好處的

network

OSS

  • Google Android
  • Android is a software stack for mobile devices that includes an operating system, middleware and key applications. This early look at the Android SDK provides the tools and APIs necessary to begin developing applications on the Android platform using the Java programming language.
  • os161 file list

overall

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美精品一区三区| 国产精品激情| 亚洲日本一区二区三区| 欧美亚洲免费高清在线观看| 亚洲视频在线观看视频| 欧美国产1区2区| 午夜精品久久久久久久蜜桃app | 这里只有视频精品| 一区二区高清视频| 午夜精品理论片| 久久久久久亚洲精品不卡4k岛国| 裸体一区二区三区| 亚洲人体一区| 校园春色综合网| 欧美大尺度在线| 国产精品久久久久一区| 黑人操亚洲美女惩罚| 亚洲国产婷婷| 午夜免费在线观看精品视频| 久久三级视频| 亚洲美女性视频| 欧美一区观看| 欧美精品在线极品| 国外成人在线| 亚洲自拍偷拍网址| 欧美激情精品久久久久久久变态 | 亚洲网站在线观看| 麻豆乱码国产一区二区三区| 欧美色综合天天久久综合精品| 国产欧美日韩一区二区三区在线观看 | 亚洲精品一二区| 亚洲免费人成在线视频观看| 久久天天综合| 亚洲视频在线看| 亚洲综合大片69999| 国内欧美视频一区二区| 日韩视频在线观看| 久久手机精品视频| 一区二区三区精品在线| 久久综合亚州| 韩国福利一区| 香蕉久久夜色| 一本到12不卡视频在线dvd| 老司机免费视频久久| 国产欧美精品一区aⅴ影院| 夜夜嗨av一区二区三区四季av| 久久久久成人网| 亚洲影院在线观看| 国产精品成人v| 一区二区三区精品久久久| 亚洲电影免费观看高清完整版在线观看| 亚洲一区久久| 国产精品美女午夜av| 亚洲一区二区视频在线| 亚洲区中文字幕| 欧美精品一卡| 在线亚洲精品| 亚洲最新合集| 欧美色网在线| 亚洲欧美在线x视频| 亚洲少妇最新在线视频| 国产精品久久久久久久久久久久久久 | 欧美精品色一区二区三区| 亚洲高清二区| 久久日韩粉嫩一区二区三区| 午夜精品久久久久99热蜜桃导演| 欧美视频一区二| 亚洲男人第一av网站| 中文在线资源观看视频网站免费不卡| 欧美日韩国产成人在线观看| 夜夜躁日日躁狠狠久久88av| 亚洲日本一区二区三区| 欧美日韩综合久久| 亚洲欧美日韩一区二区在线| 99在线精品视频在线观看| 国产精品裸体一区二区三区| 欧美一级夜夜爽| 欧美在线亚洲综合一区| 亚洲成人在线免费| 亚洲欧洲精品成人久久奇米网| 欧美日韩在线精品| 欧美在线视频一区二区三区| 欧美一级视频免费在线观看| 一区精品在线| 亚洲精品在线三区| 国产精品亚洲视频| 欧美成人第一页| 欧美午夜在线视频| 久久久久久久久久看片| 久久婷婷影院| 亚洲欧美不卡| 国内精品国语自产拍在线观看| 欧美网站在线观看| 欧美一区二区播放| 久久综合狠狠综合久久综青草| 亚洲日本中文字幕| 亚洲一区二区三区在线视频| 国内精品福利| 亚洲蜜桃精久久久久久久 | 国产午夜精品在线| 亚洲电影中文字幕| 国产精品萝li| 亚洲国产精品va| 国产日韩精品在线| 亚洲精品国产精品国自产观看浪潮| 国产精品免费看片| 亚洲电影在线观看| 国一区二区在线观看| 一区二区三区视频在线| 亚洲高清不卡| 欧美一区久久| 午夜精品久久久久久久久| 欧美高清在线视频| 久久免费少妇高潮久久精品99| 欧美日韩一区二区三区| 欧美成熟视频| 韩国亚洲精品| 亚洲欧美激情视频| 亚洲自拍啪啪| 欧美视频一区在线| 亚洲美女黄网| 99re视频这里只有精品| 久久大综合网| 欧美在线视频播放| 国产精品久久网站| 亚洲精品久久视频| 亚洲精选视频免费看| 久久天天躁狠狠躁夜夜av| 久久久精品国产免费观看同学 | 狼狼综合久久久久综合网| 欧美一二三视频| 国产精品毛片在线| 中日韩高清电影网| 亚洲影视综合| 国产精品高潮呻吟久久av黑人| 亚洲欧洲日本在线| 一级成人国产| 欧美日韩综合在线免费观看| 99国产成+人+综合+亚洲欧美| av成人动漫| 欧美网站大全在线观看| aⅴ色国产欧美| 亚洲男人的天堂在线aⅴ视频| 国产精品国产三级国产aⅴ浪潮| 一本久道久久综合狠狠爱| 亚洲永久免费观看| 国产精品国产三级国产普通话99| 99国产精品视频免费观看一公开| 日韩亚洲欧美成人| 欧美亚一区二区| 午夜精品福利在线| 美日韩丰满少妇在线观看| 在线观看亚洲a| 欧美成人国产| 亚洲一区黄色| 亚洲欧美日本在线| 久久精品首页| 在线日韩视频| 欧美日韩高清在线播放| 亚洲桃花岛网站| 麻豆精品视频在线| 99视频日韩| 国产伦精品一区二区三区四区免费| 欧美一区二区在线看| 欧美激情导航| 午夜精品成人在线| 在线观看日韩av先锋影音电影院| 欧美另类综合| 欧美一级二区| 亚洲日本国产| 欧美在线观看网站| 亚洲激情一区二区三区| 国产精品久久久久久久久久妞妞 | 国产伦理一区| 免播放器亚洲| 亚洲自拍偷拍色片视频| 噜噜噜久久亚洲精品国产品小说| 亚洲美女在线观看| 国产伪娘ts一区| 欧美日本在线一区| 久久精品日韩| 99pao成人国产永久免费视频| 欧美一区二区三区在线看| 亚洲激情影院| 国产视频一区二区三区在线观看| 久久久噜噜噜久久人人看| av成人黄色| 亚洲激情视频| 男人插女人欧美| 久久成人精品| 一区二区日韩精品| 亚洲国产日本| 黄色成人在线免费| 国产精品久久久久久久7电影| 麻豆freexxxx性91精品| 欧美日韩影院| 亚洲欧美成人一区二区三区| 亚洲电影av| 久久久免费观看视频| 午夜在线观看免费一区|