Nathan Osman
Nathan Osman

Reputation: 73195

Absolutely bizarre compiler behavior - constructor not getting called.

I have a class named NitroShare::FileServer that has two constructor:

FileServer(QObject * parent = NULL);
FileServer(const Certificate & certificate, QObject * parent = NULL);

The implementation of the first constructor is here (line 29):

FileServer::FileServer(QObject * parent)
    : QObject(parent), d(new FileServerPrivate(this))
{
    qDebug("Constructor invoked!");
}

This class is a member of another class named NitroShareClient:

NitroShare::FileServer server;

So, one would assume that the first constructor above would be called when an instance of the NitroShareClient class is created. However, it isn't. And consequently, the program crashes.

Here's where things go from frustrating to truly bizarre: if I replace the constructor declaration in the first line above with:

FileServer(QObject * parent = NULL) : d(NULL) { qDebug("Constructor invoked!"); }

...the constructor actually is invoked! Same signature. Same parameters. Same body. The only difference is the location of the function definition.

Can anyone explain what's going on here? If it helps, I'm using GCC 4.7 on Linux.

Upvotes: 1

Views: 152

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

Credits technically should go to Mark B and jogojapan for pointing out the problem. The compiler was using the constructor for a different FileServer class found here violating the one-definition rule.

Upvotes: 5

Related Questions