程序如下:
#include<iostream.h>
#include<string.h>
class a
{
public:
a(){}
a(char *q)
{
p=new char[strlen(q)+1];
strcpy(p,q);
}
a(const a &other)
{
this->p=new char[strlen(other.p)+1];
strcpy(this->p,other.p);
}
~a()
{
delete p;
}
virtual void disp()
{
cout<<p<<endl;
}
private:
char *p;
};
class b:public a
{
public:
b(){}
b(char *m,char *n):a(m)
{
s=new char[strlen(n)+1];
strcpy(s,n);
}
b(const b &other)
{
s=new char[strlen(other.s)+1];
strcpy(s,other.s);
}
~b()
{
delete s;
}
void disp()
{
cout<<s<<endl;
}
private:
char *s;
};
int main()
{
a m("123");
b n("789","456");
a *p=&m;
p->disp();
p=&n;
p->disp();
return 0;
}
定义了m是123 再在n中初始化后 为什么没变成789啊
怎么才能变成789啊?
请大家把我问题的本质讲一讲吧 我对概念还不太理解
谢谢大家了