How to check if a variable is initialized?

Greetings, I have the following situation:
1
2
3
4
5
6
7
8
class AbstractSCP *scp;

doConnect(scp) {...} // this function is NOT called, it just exists to establish a connection if needed. scp gets initilized in here

checkConnected(scp) {
if (!scp) { popMessage("Not connected!"); } // this always gets evaluated as true, why?  What's the correct way to check this?
else { doSomething(scp); } // And since the first one evaluates as true, this here crashes the program.
}

I tried adding a member bool AbstractSCP::connected and set it to false when initializing the constructor, but it still evaluates as true because the constructor is not called. (I assume?)
I tried using if(scp == NULL) { popMessage("Not connected"); } without success. Any responses are appreciated!
Last edited on
closed account (o1vk4iN6)
You are just creating a pointer to an abstract class, you need to allocate memory for it which in turn calls it's constructor.

 
AbstractSCP *scp = new DerivedSCP();

I managed to workaround with a constructor that doesn't require arguments (the constructor for AbstractSCP requires hostname, username, password and port). So I just created a default one
as
1
2
3
4
AbstractSCP::AbstractSCP()
{
        this->connected = false;
}

and then
scp = new AbstractSCP;
in my main function. Thanks for the heads-up about the pointer.
Last edited on
Topic archived. No new replies allowed.