inheritance - C++ Seg fault on reference to stored base class pointer -
I get some dirty partition error through the G ++ compiler on the following code. That would be such a thing and how to fix it, it would be great.
#include & lt; Iostream & gt; using namespace std; Class base {public: base () {} virtual ~ base () {}; Virtual Int Nine (0) = 0; }; Category received: Public base {Public: Derivative (): Base () {} ~ Removed () {} int getNum (int num) {return num; }}; Class Fu {public: Fu () {}; Zero init () {generated n; * BidID = N; } Zero other stuff () {cout & lt; & Lt; "Number is" & lt; & Lt; BaseID- & gt; Milan (14) & lt; & Lt; BaseID- & gt; Milan (15) & lt; & Lt; BaseID- & gt; Milan (16) & lt; & Lt; BaseID- & gt; Milan (15) & lt; & Lt; Endl; } Derived * baseId; }; Int main () {Foo F; F.init (); F.otherStuff (); Return 0; }
zero init () {generated n; * BidID = N; }
In addition to what Neil had noted, the derived n
is local in your init function. When you exit the function "dies", even if you have correctly specified it, this will not work.
You are not assigning a stack to whatever you want, but on the stack:
zero init () {baseId = new derived (); }
Or even better:
zero init () {remove baseId; BidID = New Derivative (); }
A constructor and constructor pair to prevent problems:
Foo (): baseId (0) {}; ~ Foo () {remove baseId; }
If going to this method, make sure to copy the block constructor and assignment operator or apply them Although properly implemented, you will need to implement a duplicate - or even derivative: use safe to store pointer.
Comments
Post a Comment