- 论坛徽章:
- 0
|
请看代码
1: 普通类---------------------------------
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
using namespace std;
class A
{
int a;
public:
A(int aa = 19) : a(aa){ }
int get_value_A() const { return a; }
};
class B: public A
{
public:
B() : A(40) { }
int get_value()
{
return get_value_A();
}
};
int main(int argc, char* argv[])
{
B b;
printf("%d\n", b.get_value());
return 0;
}
2: 模板类 -------------------------头文件----inherit_tem.h-------
#ifndef _AAAAAAAAAAA_
#define _AAAAAAAAAAA_
#include <iostream>
using namespace std;
template<class T, int ret = 6>
class A
{
T a;
int b;
public:
A(T& aa = 0) : a(aa), b(ret){ }
void print() const
{
cout << "A: " << a << endl;
}
int count() const{ return b; }
};
template<class T>
class B : public A<T>
{
public:
B(T& aa) : A<T>(aa){ }
void Bprint() const
{
A<T>::print();
}
void print_int() const
{
cout << "print_int " << A<T>::count() << endl;
}
};
#endif
-------------------源文件------------------
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "inherit_tem.h"
using namespace std;
int main(int argc, char* argv[])
{
int bb = 3;
B<int> b(bb);
b.Bprint();
b.print_int();
return 0;
}
.....................................................................
........................................................................
在继承模板类中 子类调用父类函数 为什么必须要加 父类作用域 A<T>:: 否则编译不过 ,而 普通类继承却不用加父类作用域
请教 我用的是g++ 4.4.5 GNU project C++ compiler |
|