public继承普通类,子类中可以直接用父类的protected和public成员
class base
{
protected:
int a;
typedef int value_type;
};
class child : public base
{
public:
void set(int o) { a = o; }
void print()
{
cout << a << endl;
value_type i = a;
cout << i << endl;
}
};
public继承模板类:
1.得用父类::
来使用父类的成员(或者using
语法一次性引用),直接使用将报错undefined
2.另外typedef的using语法需要添加typename
template <typename T>
class base
{
protected:
T a;
};
template <typename T>
class child : public base<T>
{
protected:
T b;
using base<T>::a; // 注意这里 1
typedef int value_type;
public:
void set(T o) { a = o; }
void print() { cout << a << endl; }
};
template <typename T>
class childchild : public child<T>
{
protected:
using child<T>::a; // 注意这里 1
using child<T>::b; // 注意这里 1
using typename child<T>::value_type; // 注意这里 2
public:
void set(T o)
{
a = o;
b = o;
}
void print()
{
cout << a << ' ' << b << endl;
value_type i = a;
cout << i << endl;
}
};