我有一堂课如下:
class base
{
protected:
int x;
int y;
int z;
public:
base(int x, int y, int z)
{
x = x;
y = y;
z = z;
}
virtual void show();
};
我从上面派生一个类:
class derived : protected base
{
public:
int a;
int b;
int c;
derived(int a, int b, int x, int y, int z) : base(x, y, z) //initialising the base class members as well
{
cout<<a<<b<<x<<y<<z; //works fine
a = a;
b = b;
}
void show()
{
cout<<a<<b<<x<<y<<z; //show junk values
}
//some data members and member functions
};
在 main() 中,我使用:
derived d(1, 2, 3, 4, 5);
d.show();
数据成员似乎在构造函数中具有合法值。但是,当我使用类似的功能,即使用相同的可见性模式时,似乎会出现垃圾值。