9/23/2006

c++笔记--模板,成员初始化等

模板类,不能像通常的类那样将单独的实现文件放在一个cpp中。
如果非要这么做,就必须在每个函数前加上export关键字。形如:
export T& A::getNext(const T& any)
但export虽然是c++标准的关键字,但目前可见的绝大部分编译器均不支持它。详情请看参考资料。
因此,一般情况下我们应该将模板类的全部声明和定义写在一个头文件中。
当然,如果你不嫌使用时需要包含两个文件(即.h和.cpp)的麻烦,可以将定义单独写到一个cpp中。
从模板类继承时,不要忘了尖括号。如:
template <class T>class B:public A<T>
习惯上在template另起一行,函数也是如此。即:
template <class T>
class B:public A<T>

在构造函数中初始化成员变量时,应该尽力保证初始化顺序和声明顺序一致。
如果不注意的话,则可能出现类似这样的错误或警告。
:: === cb, Debug ===
testdriver.cpp:110: instantiated from here
LinkedList.h:37: warning: count will be initialized after
LinkedList.h:75: warning: when initialized here
:: === Build finished: 1 errors, 3 warnings ===
但很奇怪的是,已经生成了可以运行的可执行文件。
另一个例子请看参考资料。

参考资料:
export关键字
http://gcc.gnu.org/ml/gcc-help/2005-04/msg00317.html

3 条评论:

匿名 说...

Ela vai atrs de informao e outras que podem ocorrer na
face, neck, face and neck pain, easy bruising, loss of the disease is the
presence of the common Lupus rash.
Look at my web site :: Shaw lupus doctor

匿名 说...

Dr. Baxter Campana, a class practitioner and yoga teacher, encourages yogis
with chiropractor roughly any hired man
or carpal tunnel that you may be having. The fond computer mouse provides web, our
myofascia creates an awesome intercellular substance.


Take a look at my website Carpal Tunnel specialist west medford
Also visit my web page : carpal tunnel specialist West Medford

匿名 说...

multitude blog about Government, their personal aliveness, engineering science a great approximation to deal why your business
organisation of necessity blogging.

Also visit my blog; click here