Hi,
I'm trying to play with an abstract base class and 3 derived classes in a linked list.
But when i began to design constructors and destructors for the classes i just hit a wall.
So i'll simplify the code to be more relevant, wit just 1 derived class:
My question is where can I delete[] bstxt ?Code:class Base // abstract base class; no obj can be created for this class { protected: int bsnr; char* bstxt; //must be allocated with new & initialized public: Base() : bsnr(0) // default constructor { bstxt = new char[size]; // allocation for a string of 'size' strcpy(bstxt, "smth"); // initialisation } X virtual ~Base()=0 // virtual destructor, from what i read i can't give any definition here virtual func()=0 // pure virtual function }; class Derived: public Base { protected: int drvnr; char* drvtxt; // also mem allocation whit new pubilc: Derived() : drvnr(0) { bstxt = new char[size]; // allocation for a string of 'size' strcpy(bstxt, "smth"); // initialisation } ~Derived() { Y delete[] drvtxt; } func(arg){ do something with bsnr, bstxt, drvnr, drvtxt, } };
Am i allowed to do it in line X? like:
or can I free memory for a Base member in a deriver class?Code:virtual ~Base()=0 { delete[] bstxt; }
Any hint or ideea is welcome.Code:~Derived() { delete[] bstxt; delete[] drvtxt; }
Thank you.



LinkBack URL
About LinkBacks




CornedBee