| aclark17 (16) | |
|
// see main() #include <iostream> #include<string> using namespace std; class x { public: x() { a++; } private: static int a; }; int x::a=0; class y:public x { public: y(x& tx=x(), string* tb=0):x(tx),b(tb) { } y(y& rhs) { *this=rhs; } virtual y& operator=(y& rhs) { (x&)*this=(x&)rhs; b=new string[6]; for(int i=0;i<6;i++) b[i]=rhs.b[i]; return *this; } ~y() { delete[]b; } private: string* b; }; class z:public y { public: z(y& ty=y(),int n=0):c(n) { (y&)*this=ty; } z(z& s) { *this=s; } virtual z& operator=(z& rhs) { (z&)*this=(z&)rhs; return *this; } private: int c; }; void main() { string *h=new string[6]; string *h1=new string[6]; for(int i=0;i<6;i++) h[i]=h1[i]="a"; // this line is making 2 calls to base class a ctor // cant figure.out why or how to solve the problemproblem z s1(y(x(),h)); } | |
|
|
|
| ne555 (4041) | |||
[code]"Please use code tags"[/code]Your code shall not compile. ( int main(), the references should be const)And have several bad practices (dynamic allocation abuse, bad memory handling, casting, obfuscation) As to why x::a is 2.
| |||
|
|
|||
| aclark17 (16) | |
| How should i solve thos problem virtual keywords global varibles | |
|
|
|