PDA

View Full Version : Overloading QMap << operator



The 11th plague of Egypt
14th September 2011, 16:54
I don't remember very well how to overload operators in a template, so I humbly hope somebody can help me with this.

I'd like to do something like this


Contact contact("Name", "Surname");
QMap<QString, Contact> map;
map.insert("first", contact);
qDebug() << map;

and have an output like this


first: Name Surname

This is a just a basic example to help me understand templates, I know there are simpler ways, like a for loop.
BTW is it possible to have a different overloading of << for QMap<QString, Contact> and QMap<Contact, QString> ?

high_flyer
14th September 2011, 17:12
I don't remember very well how to overload operators in a template
Ok.
So post the overloading for a regular class then, and we'll go from there.

The 11th plague of Egypt
14th September 2011, 17:50
Hum, I know that the best way to overload the << operator is using an external function.


ostream& operator<<(ostream &os, const myClass &mc)
{
return os << myClass.whatever;
}

And declare that function as a friend of the class


Class myClass
{
friend ostream& operator<<(ostream &os, const myClass &mc);
//etc
};


I know templates are a little different though, and I don't know if I can use qDebug() just like an STL ostream.

wysota
14th September 2011, 20:24
Providing support for the QDebug stream operator