I have a class Base
and another class Derived
(derived from Base
).
Also I have a 2 functions GetValue(Base*)
and GetDerivedValue(Derived*)
.
Now, from the main()
function, I have access to only GetValue()
, but I need the value of both Base
and Derived
classes.
class Base
{
public:
Base ()
{
abc =0;
}
~Base ()
{
abc =0;
}
int abc;
};
class Derived : public Base
{
public:
Derived ()
{
def =0;
}
~Derived ()
{
def =0 ;
}
int def;
};
int GetDerivedValue (int &def)
{
def = 5;
}
int GetValue (Base *&pbase)
{
Derived *d = new Derived ();
GetDerivedValue (d->def);
pbase = (Base *) d;
}
int main ()
{
Base *pbase = NULL;
GetValue (pbase);
// Goal here is to get the avlue of def as 5 without any memory issue
}
Where should I release the pointers to make sure there is no memory errors?