(请教)继承普通类和模板类的一个问题(关于类作用域)

chenbin200818 chenbin200818 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
请看代码

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
...全文
评论 12
收藏 已收藏
分享
返回顶部
chenbin200818
C++ 语言 65,214 成员 250,514 内容