tags:

views:

120

answers:

2

If for no other reason than for my own amusement, I wish to write a global insertion operator so I can use the fancy code:

aQMenu << aQAction1 << aQAction2 << aQAction2 << seperator << aQAction3;

Perhaps you hate the syntax, but I would at least like to try my hand at using it. The problem is, that this is the first time I have tried to write insertion operator code, and I am stumped. The code for inserting the enum "seperator" to the QMenu* is easy, and I have that working, but I thought this code would work for inserting a QAction* to a QMenu*:

// does not compile:  "must have an argument of class or enumerated type"
QMenu *operator<< (QMenu *menu, QAction *action)
{
        menu->addAction(action);
        return menu;
}

The compiler complains saying this function needs an argument of a class or enumerated type, which confounds me because the second parameter is of class type.. I have tried to rephrase this function using the ampersand, but I have not hit upon the way of writing it down properly. I have looked a lot at web examples, and thought it is about time to just as the question here.

I know that some coders out there will complain about me deviating from standard Qt syntax, but I am having fun overloading the operator<< with other classes as well. It just seems that the insertion operator works nicely here. What can I say -- it makes me happy.

+4  A: 

The compiler complains because all the parameters are pointers. Using a reference for the menu parameter of the operator it would look like this:

QMenu& operator<< (QMenu &menu, QAction *action) {
    menu.addAction(action);
    return menu;
}

Now this operator should work on menu objects. If you want to use it with a pointer to a menu you need to dereference that pointer when applying the new << operator:

QMenu *menu = new QMenu();
QAction *action1, *action2;
...
*menu << action1 << action2;
sth
Can anyone recommend a *comprehensive* guide to operator overloading. I have read a few trivial ones, but nuances such as what I am encountering are not discussed well at all.
Vance Tower
A: 

Everything works fine now. I use the code

QMenu& operator<< (QMenu &menu, QAction *action) {
    menu.addAction(action);
    return menu;
}

so I can juggle my QMenu with the nice little syntax of

*mnuFile << actNewFile << actOpenFile << actCloseFile << seperator << actExit;

(another code snippet lets the insertion operator take my "seperator" argument).

Also, I read on these forums that Andrei Alexandrescu devotes quite a few pages on smart logger singletons in "Modern C++ Design". I am still trying to grasp while my operator<< code

QMenu* operator<< (QMenu* obj, MyMenuInsertEnum val);

is valid (the first parameter is a pointer). But this is not:

QMenu *operator<< (QMenu *menu, QAction *action);

And the problem is just the pointer as the first argument. After I read the aforementioned book then I will append my findings here, providing I learn something new.

Vance Tower