-2

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • The general rule of thumb on when to `delete`: When you're done with the object and no sooner. This [smurf](https://en.wiktionary.org/wiki/bowdlerize) gets difficult as the programs you write gain complexity, so the trick the masters use is to almost never use `new` and `delete`. Instead they use Automatic Allocations and [RAII-compliant containers](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) like `std::vector` and [smart pointers](https://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i-use-one). – user4581301 Aug 23 '21 at 18:30
  • 1
    When the master C++ programmer is forced to dynamically allocate storage, they immediately wrap the allocation with RAII so no one else has to worry about the allocation. Rule of thumb: Keep the management of resources as close as possible to the resource. – user4581301 Aug 23 '21 at 18:33
  • There is something odd about the way you wrote your question. I got to the last line and was surprised that the question was about memory management instead of inheritance. I'm not sure what advice to give for improving it, but there is certainly something off in the presentation, maybe in the thinking that led to the question. – JaMiT Aug 23 '21 at 23:04

1 Answers1

2

I think you are looking for something like this:

class Base
{
public:
    Base() : abc(0) {}
    virtual ~Base() {}
    int abc;
};

class Derived : public Base
{
public:
    Derived() : def(0) {}
    ~Derived() {}
    int def;
};

void GetDerivedValue(int &def)
{
    def = 5;
}

void GetValue(Base* &pbase)
{
    Derived *d = new Derived;
    GetDerivedValue(d->def);
    pbase = d;
}

int main()
{
    Base *pbase = NULL;
    GetValue(pbase);
    // use pbase as needed...
    delete pbase;
}

Which would be better written using std::unique_ptr instead:

#include <memory>

class Base
{
public:
    virtual ~Base() = default;
    int abc = 0;
};

class Derived : public Base
{
public:
    int def = 0;
};

void GetDerivedValue(int &def)
{
    def = 5;
}

std::unique_ptr<Base> GetValue()
{
    auto d = std::make_unique<Derived>();
    GetDerivedValue(d->def);
    return d;
}

int main()
{
    auto pbase = GetValue();
    // use pbase as needed...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770