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

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 閱讀(643) 評論(0)  編輯 收藏 引用 所屬分類: Template
<2025年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用鏈接

留言簿(10)

隨筆分類(307)

隨筆檔案(297)

algorithm

Books_Free_Online

C++

database

Linux

Linux shell

linux socket

misce

  • cloudward
  • 感覺這個博客還是不錯,雖然做的東西和我不大相關,覺得看看還是有好處的

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

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            性8sex亚洲区入口| 亚洲每日更新| 欧美一区二区三区免费观看视频| 亚洲国产日韩欧美在线99| 欧美中文字幕| 亚洲国产欧美不卡在线观看| 欧美成人dvd在线视频| 久久综合九色九九| 亚洲精品久久久一区二区三区| 亚洲国产精品va在线看黑人| 欧美精品日韩综合在线| 亚洲婷婷免费| 欧美一区三区二区在线观看| 极品尤物久久久av免费看| 亚洲高清资源| 国产精品久在线观看| 久久在线观看视频| 欧美成人免费一级人片100| 亚洲婷婷在线| 久久久www免费人成黑人精品| 亚洲激情中文1区| 在线视频日本亚洲性| 韩国精品一区二区三区| 亚洲欧洲一区二区在线播放| 国产精品乱子乱xxxx| 久久综合色播五月| 欧美日韩国产首页| 久久九九全国免费精品观看| 欧美成人中文字幕| 久久国产主播精品| 欧美激情国产高清| 亚洲电影免费观看高清完整版| 欧美激情第五页| 国产精自产拍久久久久久蜜| 欧美国产综合| 国产一区二区三区高清 | 国产性天天综合网| 亚洲国产精品嫩草影院| 国产亚洲综合在线| 一二三四社区欧美黄| 亚洲二区视频在线| 亚洲欧美久久久| 宅男噜噜噜66一区二区66| 久久久综合激的五月天| 久久国产欧美日韩精品| 欧美色精品在线视频| 亚洲国产精品成人综合色在线婷婷| 国产精品一级二级三级| 99精品视频免费观看| 亚洲黄色片网站| 久久五月婷婷丁香社区| 久久久精品日韩| 国产精品视屏| 亚洲一区二区三区中文字幕在线 | 久久国产精品网站| 欧美一级在线亚洲天堂| 欧美日韩亚洲高清一区二区| 亚洲国产精品第一区二区| 今天的高清视频免费播放成人| 亚洲欧美国产毛片在线| 亚洲欧美日韩国产综合| 国产精品theporn| 日韩午夜电影在线观看| aa级大片欧美| 欧美日韩国产首页| 日韩亚洲在线| 亚洲性视频h| 欧美午夜在线观看| 亚洲一级高清| 久久都是精品| 红桃视频一区| 麻豆精品视频在线观看| 欧美成人a∨高清免费观看| 在线精品福利| 欧美刺激性大交免费视频| 亚洲高清网站| 中文日韩在线| 国产精品揄拍一区二区| 久久riav二区三区| 欧美国产日韩一二三区| 亚洲精品乱码久久久久久日本蜜臀| 蜜臀91精品一区二区三区| 亚洲国产日韩综合一区| aa级大片欧美| 国产乱码精品一区二区三区五月婷| 午夜精品久久久久久久久 | 亚洲国产专区| 欧美日韩久久精品| 亚洲欧美日韩一区| 蜜月aⅴ免费一区二区三区| 亚洲人妖在线| 国产精品久久久久久久浪潮网站| 亚洲免费视频中文字幕| 久热国产精品| 一区二区欧美精品| 国产日韩专区在线| 欧美chengren| 亚洲欧美国产日韩天堂区| 老司机免费视频一区二区| 日韩午夜黄色| 国内精品伊人久久久久av一坑| 免费亚洲网站| 午夜在线视频观看日韩17c| 欧美aa在线视频| 亚洲欧美日韩在线观看a三区| 狠狠色丁香婷综合久久| 欧美激情综合五月色丁香小说| 亚洲一区免费观看| 亚洲国产精品女人久久久| 欧美一区二区三区喷汁尤物| 亚洲精品国精品久久99热| 国产欧美日韩高清| 欧美日韩国产精品一区二区亚洲| 欧美自拍偷拍| 亚洲午夜激情在线| 欧美激情一区二区三区在线视频观看 | 亚洲欧美美女| 亚洲狼人精品一区二区三区| 久久久免费av| 欧美一区二区黄| 一区二区三区视频在线播放| 一区二区视频欧美| 国产欧美韩日| 国产精品久久久久免费a∨| 欧美成人性网| 久久综合久久综合久久| 欧美一区二区大片| 亚洲桃花岛网站| 日韩视频不卡中文| 亚洲国产精品成人| 女人天堂亚洲aⅴ在线观看| 欧美主播一区二区三区美女 久久精品人 | 国产精品你懂的在线欣赏| 欧美乱妇高清无乱码| 欧美大片在线看免费观看| 久久综合色88| 久久夜色精品国产欧美乱| 久久久久久久97| 久久精品国内一区二区三区| 午夜日韩av| 午夜视频在线观看一区| 午夜精品一区二区三区电影天堂| 一本大道av伊人久久综合| 亚洲精品免费一区二区三区| 亚洲精华国产欧美| 亚洲精品一区二区三区樱花| 亚洲精品你懂的| 99国产精品自拍| 亚洲视频香蕉人妖| 亚洲欧美日韩久久精品| 性欧美长视频| 久久久久久有精品国产| 久久在线免费观看视频| 免费观看日韩av| 欧美人交a欧美精品| 国产精品成人观看视频免费| 国产精品久久久久久久久久久久久| 国产精品h在线观看| 国产老女人精品毛片久久| 国产亚洲综合在线| 亚洲国产精品一区在线观看不卡| 亚洲欧洲久久| 亚洲已满18点击进入久久| 久久精品国产亚洲aⅴ| 久久久五月婷婷| 亚洲福利在线视频| 亚洲视频免费观看| 欧美专区日韩专区| 欧美黑人多人双交| 国产精品亚洲成人| 在线成人中文字幕| 亚洲网站在线| 久久男人av资源网站| 亚洲激情欧美| 亚洲欧美日韩国产中文| 久久婷婷av| 国产精品日韩欧美大师| 一区视频在线| 亚洲综合电影一区二区三区| 久久亚裔精品欧美| 99国产麻豆精品| 久久久久久久网站| 欧美午夜视频一区二区| 尤物九九久久国产精品的分类| 一区二区三区精品视频| 久久青草欧美一区二区三区| 亚洲激情视频在线| 久久av一区二区三区漫画| 欧美人与性动交α欧美精品济南到| 国产精品人人做人人爽人人添| 一区二区在线不卡| 午夜精品久久久久| 亚洲激情网站| 久久男女视频| 国产精品丝袜久久久久久app| 亚洲破处大片| 久久综合九色综合久99| 亚洲一区视频| 欧美体内谢she精2性欧美| 亚洲国产成人久久|