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

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>
            欧美影视一区| 亚洲国产日韩欧美| 美女日韩在线中文字幕| 亚洲风情亚aⅴ在线发布| 99综合在线| 在线观看视频免费一区二区三区| 亚洲精品国产精品国自产观看浪潮 | 欧美三日本三级少妇三2023| 久久久久久久波多野高潮日日| 欧美高清一区二区| 久久成人免费| 欧美色图麻豆| 欧美激情一区在线| 国产偷自视频区视频一区二区| 最新国产乱人伦偷精品免费网站 | 亚洲毛片在线观看| 欧美一区二区高清在线观看| 一区电影在线观看| 这里只有视频精品| 亚洲三级性片| 日韩视频永久免费观看| 亚洲国产精品一区二区三区| 午夜在线观看欧美| 亚洲免费在线看| 欧美激情一区二区三区不卡| 男女av一区三区二区色多| 国产视频久久| 亚洲一区二区高清| 亚洲深夜激情| 欧美精品免费看| 农村妇女精品| 一区二区三区在线视频免费观看| 亚洲女人天堂av| 亚洲欧美美女| 国产精品v欧美精品v日韩精品| 亚洲国产精品激情在线观看| 亚洲国产mv| 久久夜色撩人精品| 免费欧美日韩| 亚洲第一在线| 蜜桃精品久久久久久久免费影院| 欧美va天堂| 国产在线精品自拍| 久久久久9999亚洲精品| 欧美v国产在线一区二区三区| 国产精品久久久久7777婷婷| 99国产精品久久久久久久| 在线视频精品一| 久久久久天天天天| 久久婷婷国产综合国色天香| 国语自产偷拍精品视频偷 | 欧美日韩国产综合视频在线观看| 亚洲国产精品成人久久综合一区| 最新中文字幕亚洲| 欧美精品免费观看二区| 国产精品福利网| 亚洲视频在线一区观看| 欧美日韩综合一区| 一片黄亚洲嫩模| 一本久道久久久| 国产精品久久久一区麻豆最新章节 | 午夜日韩在线观看| 久久影视精品| 亚洲日本视频| 欧美午夜精品一区| 午夜日韩激情| 欧美xxxx在线观看| 夜夜精品视频一区二区| 国产精品国产三级国产| 欧美一区二区三区四区夜夜大片| 美女视频黄a大片欧美| 香蕉久久夜色| 国产一级久久| 老司机精品视频网站| 亚洲三级免费电影| 亚洲一区在线免费观看| 国内欧美视频一区二区| 欧美国产日韩一区| 亚洲一区二区三区免费观看| 久久午夜影视| 一区二区日韩欧美| 国产欧美日韩亚洲精品| 欧美在线资源| 99精品国产在热久久婷婷| 久久九九精品| 亚洲最黄网站| 国模私拍视频一区| 欧美精品成人在线| 香蕉久久夜色| 亚洲人午夜精品免费| 欧美在线日韩| 99re8这里有精品热视频免费| 国产精品伦理| 欧美国产日产韩国视频| 欧美一区在线直播| av72成人在线| 欧美大尺度在线观看| 性色一区二区| av不卡在线观看| 伊甸园精品99久久久久久| 国产精品成人va在线观看| 免费成人黄色片| 欧美在线视频播放| 一本色道**综合亚洲精品蜜桃冫| 母乳一区在线观看| 久久黄色小说| 亚洲欧美日韩网| 一本大道久久a久久精品综合| 激情国产一区| 国产拍揄自揄精品视频麻豆| 欧美日韩你懂的| 欧美一区二区女人| 一本色道久久综合亚洲91| 亚洲电影第三页| 久久久欧美精品| 亚洲一区二区三区在线| 亚洲美女在线看| 亚洲青涩在线| 亚洲国产裸拍裸体视频在线观看乱了中文 | 欧美日本韩国| 久久精品视频免费播放| 一区二区三区精品视频在线观看| 农村妇女精品| 女同一区二区| 欧美成人午夜影院| 欧美大片一区| 亚洲国产成人tv| 欧美国产日韩一区| 欧美成人一区二区三区片免费| 久久综合中文字幕| 另类激情亚洲| 欧美成人免费视频| 亚洲国产精品久久久久秋霞蜜臀 | 久久久精品一区二区三区| 欧美一区二区三区喷汁尤物| 欧美一区不卡| 久久久久久9| 免费人成网站在线观看欧美高清| 蜜桃av一区二区三区| 欧美二区在线看| 亚洲国产精品久久精品怡红院| 亚洲国产一区视频| 欧美激情一区二区三区成人| 亚洲成人在线网| 欧美大片免费看| 亚洲国产精品久久久久婷婷老年 | 亚洲福利视频三区| 99riav久久精品riav| 亚洲网站在线观看| 香蕉成人啪国产精品视频综合网| 久久久久久尹人网香蕉| 欧美黄污视频| 在线视频精品一| 99伊人成综合| 亚洲欧美另类国产| 久久爱www.| 欧美日本国产精品| 国产亚洲一本大道中文在线| 亚洲国产精品嫩草影院| 国产精品99久久久久久久女警 | 亚洲精选一区二区| 香蕉成人久久| 欧美在线免费观看| 欧美高清免费| 国产精品一香蕉国产线看观看| 黄色精品一区二区| 在线亚洲精品| 老司机一区二区三区| 小处雏高清一区二区三区| 欧美18av| 久久精品人人做人人综合| 国产精品女主播在线观看| 亚洲免费观看高清完整版在线观看| 久久精品视频一| 亚洲影音先锋| 国产精品h在线观看| 亚洲精品视频在线播放| 你懂的成人av| 久久九九热免费视频| 国产日韩欧美一区| 欧美一区二区三区四区高清| 一区二区欧美激情| 欧美日韩免费| 一本色道久久综合一区| 亚洲国产一区二区a毛片| 麻豆九一精品爱看视频在线观看免费| 韩国av一区二区三区| 久久se精品一区二区| 亚洲欧美日韩区| 国产精品亚洲综合一区在线观看| 亚洲午夜电影在线观看| 日韩视频在线一区二区| 欧美日韩精品一区二区天天拍小说| 亚洲伦理自拍| 亚洲精品系列| 国产精品chinese| 午夜精品福利在线观看| 亚洲女人小视频在线观看| 国产伦精品一区二区三区免费迷| 欧美一区在线看|