user765443
user765443

Reputation: 1892

check object existance when function retuning pass by reference

I have very basic issue and have following code. In rare cases, we are not able to call setDriver(set driver object) and call getDriver function for calling driver class function which resultant memory crash( as setdriver has not set). is it possible to check getdriver object. I have tried to set NULL but as we are returning reference it's not appropriate/feasible.

include<iostream>
#include<string>
using namespace std;

class Driver {
private:
 string name;
public:
  void setname(string name);
  void display();
};

void Driver::setname(string name)
{
  this->name = name;
}

void Driver::display()
{
  cout<<" This driver is .."<<name<<endl;
}

class sample {
protected:
Driver *m_d;
public:

void setDriver(Driver *driver);
Driver& getDriver();
};

void sample::setDriver(Driver *driver)
{
   m_d = driver;
}

Driver& sample::getDriver()
{
   return *m_d;
}

int main()
{
  sample s;
  Driver *d = new Driver;
  d->setname("test");
  s.setDriver(d);
  Driver &d1 = s.getDriver();
  // How can I check if d1 is exist or not 
  d1.display();
}

After getting input from below discussion, I have modified code into following way. will it be correct solution?

#include<iostream>
#include<string>
using namespace std;

class Driver {
private:
 string name;
public:
  void setname(string name);
  void display();
};

void Driver::setname(string name)
{
  this->name = name;
}

void Driver::display()
{
  cout<<" This driver is .."<<name<<endl;
}

class sample {
protected:
Driver *m_d;
public:

void setDriver(Driver *driver);
Driver& getDriver();
sample();
};

sample::sample()
{
  m_d = NULL;
}

void sample::setDriver(Driver *driver)
{
   m_d = driver;
}

Driver& sample::getDriver()
{
   if(m_d)
     return *m_d;
   else
         throw "Error";
}

int main()
{
  sample s;
  Driver *d = new Driver;
  d->setname("test");
  s.setDriver(d);
  try{
      Driver &d1 = s.getDriver();
      d1.display();
  }
  catch(...)
  {
    cout<<" object is not prsent"<<endl;
  }

Upvotes: 1

Views: 59

Answers (2)

Jarod42
Jarod42

Reputation: 217810

A safe way to don't have to check if pointer is null is to force it to never be:

class sample {
public:
    // No default constructor

    explicit sample(Driver &driver) m_d(&driver) {}

    void setDriver(Driver &driver) { m_d = &driver; }
    Driver& getDriver() { return *m_d; }
private:
    Driver *m_d;
};

Usage:

int main()
{
    Driver d1;
    d1.setname("test1");
    Driver d2;
    d2.setName("test2");

    sample s(d1);
    s.getDriver().display();
    s.setDriver(d2);
    s.getDriver().display();
}

Upvotes: 0

Elemental
Elemental

Reputation: 7521

References can't point to nothing so I suggest two options: a) throw an exception - this for me is clearly the best solution; this is an edge case and very appropriate for exception handling b) Include a method driver.isLoaded() which the caller can check - in the case when you can't allocate a driver return a stub driver which returns this value as false

Alternately you need to change the nature of the call to return a pointer (so that you can return null) or pass your driver as a reference parameter and return a boolean to indicate success. Neither of these options seems as good as a above to me.

Upvotes: 2

Related Questions