tags:

views:

94

answers:

1

So, I've defined a class like

DataLoggingSystemStateReceiver
{
DataLoggingSystemStateReceiver()
:   // initializer list
{
    // stuff
}

//  ... other functions here

};

In main, I instantiate DataLoggingSystemStateReceiver like so:

int main()
{
    // ... run stuff
    Sensor sensor(port, timer);

    DataLoggingSystemStateReceiver dlss();

    Log::notice("started");
    return 0;
}

However, when I step through this code in gdb, it runs:

Sensor sensor(port, timer);

skips

DataLoggingSystemStateReceiver dlss();

and continues with

Log::notice("started");

What gives?


EDIT: By changing

DataLoggingSystemStateReceiver dlss();

to

DataLoggingSystemStateReceiver dlss;

in main(), the line executes. Can someone explain why?

+9  A: 

This:

DataLoggingSystemStateReceiver dlss();

does not declare an automatic variable. It declares a function named dlss that takes no arguments and returns a DataLoggingSystemStateReceiver.

You want:

DataLoggingSystemStateReceiver dlss;

The object will be default initialized, so for your class type, the default constructor will be called.

James McNellis
Is this behavior occuring because the object's default constructor takes no arguments?
sheepsimulator
@sheep: No. This is just a shortcoming of the syntax, it's called the "most vexing parse". The compiler simply reads this as the declaration of a function, it has nothing to do with your class.
GMan
Cool. Thanks for the explanation!
sheepsimulator