PDA

View Full Version : Mess with inherited constructors



Pepe
24th August 2007, 08:56
I created a subclass of QAction, among others with this constructor:


MyAction(QObject * parent, const QString & name);


Then I use it in other parts this way:



MyAction *action = new MyAction(this, "name");


The problem is that instead of my constructor, it's called QAction ( QObject * parent, const char * name ).

I fixed it changing my constructor to


MyAction ( QObject * parent, const char * name);


But I also had the same problem with other constructors.

Is it possible any way that only my constructors are taking into account and not the inherited ones?

jpn
24th August 2007, 11:24
Could we see the implementation of the problematic constructor?

Pepe
24th August 2007, 13:34
MyAction::MyAction(QObject * parent, const QString & name) : QAction(parent)
{
setObjectName(name);
}


Anyway this code is not called.

jpn
24th August 2007, 13:47
It just... won't magically jump directly to QAction constructor if you create a MyAction. :)


#include <QtGui>
#include <QtDebug>

class MyAction : public QAction
{
public:
MyAction(QObject* parent, const QString& name);
};

MyAction::MyAction(QObject* parent, const QString& name)
: QAction(parent)
{
qDebug() << "MyAction::MyAction()";
setText(name);
setObjectName(name);
}

class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget* parent = 0);
};

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
qDebug() << "MainWindow::MainWindow()";
MyAction* action = new MyAction(this, "Action");
menuBar()->addAction(action);
}

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Pepe
25th August 2007, 09:44
I think I have found the problem. It wasn't calling QAction ( QObject * parent, const char * name ) but another constructor that I had: MyAction(QObject *, bool).

But why a "myaction *action = new MyAction(this, "name")" could call that constructor with a bool argument? I don't get it.

And the problem is that I really need that constructor.

Your code modified:



#include <QtGui>
#include <QtDebug>


class MyAction : public QAction
{
public:
MyAction(QObject* parent, const QString& name);
MyAction(QObject *parent, bool a);
};

MyAction::MyAction(QObject* parent, const QString& name)
: QAction(parent)
{
qDebug() << "MyAction::MyAction()";
setText(name);
setObjectName(name);
}

MyAction::MyAction(QObject *parent, bool a) : QAction(parent)
{
qDebug() << "MyAction::MyAction(QObject, bool)";
}

class MainWindow : public QMainWindow
{
public:
MainWindow(QWidget* parent = 0);
};

MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
qDebug() << "MainWindow::MainWindow()";
MyAction * action = new MyAction(this, "Action");
menuBar()->addAction(action);
}

int main(int argc, char* argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}



After running it, console output:


MainWindow::MainWindow()
MyAction::MyAction(QObject, bool)

jpn
25th August 2007, 10:38
The most sensible choice for a human is not always the most sensible choice for a machine. ;)

A literal "Action" is a const char*. A pointer can be directly used as a boolean value whereas for a QString to match, the compiler would have to implicitly convert const char* to a QString. This is why MyAction(QObject*, bool) gets chosen over MyAction(QObject*, const QString&). Now, if you add MyAction(QObject*, const char*) as mentioned in the first post, it gets naturally chosen as a first choice because it's the closest possible match.

bpetty
30th August 2007, 22:00
Makes me wonder what would happen if you called MyAction(this, QString("name"));

wysota
8th September 2007, 17:36
It just... won't magically jump directly to QAction constructor if you create a MyAction. :)
Actually in such situation it will call the default QAction constructor, won't it?

jpn
8th September 2007, 18:16
Actually in such situation it will call the default QAction constructor, won't it?
The question was about

new MyAction(this, "name");