请大侠解决问题:type a 于type *a = new type();的不同表现
我有一段代码如下:
////Base.h
class Base
{
public:
Base(int x):_x(x){};
virtual void print();
private:
int _x;
};
////Base.cpp
void Base::print()
{
cout<<"x = "<<_x<<endl;
}
////Derived.h
class Derived :public Base
{
public:
Derived(int y):Base(y),_y(y){};
virtual void print();
private:
int _y;
}
///////Derived.cpp
void Derived::print()
{
Base::print();
cout<<"y = "<<_y<<endl;
}
//////main.cpp
int main()
{
Base d1 = Derived(1);
Base *d2 = new Derived(0);
d1.print();
d2->print();
}
运行结果是:
x=1
x=0
y=0
为什么不是
x=1
y=1
x=0
y=0
这两种调用有什么不同?