子类不能继承父类哪些成员
子类不能从父类继承的有:
1. 构造函数
2. 拷贝构造函数
3. 析构函数
子类能从父类继承的有:
1、静态成员变量
2、静态成员函数
3、友元函数
4、赋值操作符=重载函数
关于子类不能继承的三点,这个我大概可以肯定,之前对此也有所了解。但是对其能继承的4点我就不太确认,在网上看了看,也没有找到明确的答案。我贴一下测试代码,欢迎大家指教。
#include<iostream>
using namespace std;
class base
{
int mx;
public:
static int xxx; //静态成员变量
static void fun(int x) //静态成员函数
{
xxx=x;
}
base(int x):mx(x){}
friend std::ostream& operator<<(std::ostream& ,base&); //友元函数
base& operator =(base &s) //赋值操作符=重载函数
{
this->mx = s.mx;
return *this;
}
};
int base::xxx=0;
class test:public base
{
public:
test(int x):base(x){}
};
std::ostream& operator<<(std::ostream& os,base& s)
{
os<<s.mx<<" "<<"helloworld";
return os;
}
int main()
{
test x(321);
test y(123);
cout<<test::xxx<<endl;
test::fun(999);
cout<<test::xxx<<endl;//输出999,则说明能继承静态成员函数
cout<<x<<endl;
cout<<y<<endl; //输出123,则说明继承了友元函数
y = x;
cout<<y<<endl; //输出321 helloworld,则说明继承了=赋值操作符函数
system("pause");
return 0;
}
测试结果:(加入helloworld是为了说明,使用的<<是我们自定义的,为了为系统自定义的相区别)

浙公网安备 33010602011771号