The object of my class is becoming equal to nullptr and failing. I can't seem to figure out why.
I am trying to pass a string as an argument to a constructor of a class which has been defined by another team - Hello in this example. Hello class has defined a constructor which takes a reference string as an argument.
I am trying to initialise the object of class Hello in my class Abc and call the function print.
class Hello
{
Hello(const string& email)
{
// initialises some values.
}
void print();
};
class Abc
{
struct Options
{
string email;
};
Abc(const Options& options)
{
Hello hl(options.email);
cout << "h1 initialised";
}
void callprint()
{
if (h1 == nullptr)
cout << "NULL";
else
h1->print();
}
};
int main()
{
Abc::Options op; // for structure
op.email = "hello@world.com"
Abc obj(op); // to initialise constructor of class Abc
obj->callprint();
}
The output I am getting currently is:
h1 initialised
NULL
I can't seem to figure out why h1 is becoming equal to nullptr. In C++ class objects never become null by default right?
Earlier, I wasn't checking for (h1==nullptr) condition and my program was failing. What should I do to ensure that h1 is not a null object and I can call the print function successfully.
To reiterate, Hello class has been defined by another team. I can't modify it. I can make any changes to main and Abc class though.
Aucun commentaire:
Enregistrer un commentaire