Name lookup
-
Good people, Can anybody help me with this problem: from the sample below, provided that the only code can be modified is the MyContainer class, is it possible to change the name lookup in such a way that the MyContainer::operator Element() gets called instead of the Element::Element(const AbstractContainer&) constructor? Thanks, Serge #include "stdafx.h" using std::cout; class AbstractContainer {}; class BasicContainer : public AbstractContainer {}; class Element { public: Element(const AbstractContainer&) {cout << "Bad\n";} }; class MyContainer : public BasicContainer { public: operator Element() const {cout << "Good\n";} }; int main() { MyContainer mc; // I want the MyContainer::operator Element() gets called here // instead of the Element::Element(const AbstractContainer&) constructor Element e = mc; return 0; }
-
Good people, Can anybody help me with this problem: from the sample below, provided that the only code can be modified is the MyContainer class, is it possible to change the name lookup in such a way that the MyContainer::operator Element() gets called instead of the Element::Element(const AbstractContainer&) constructor? Thanks, Serge #include "stdafx.h" using std::cout; class AbstractContainer {}; class BasicContainer : public AbstractContainer {}; class Element { public: Element(const AbstractContainer&) {cout << "Bad\n";} }; class MyContainer : public BasicContainer { public: operator Element() const {cout << "Good\n";} }; int main() { MyContainer mc; // I want the MyContainer::operator Element() gets called here // instead of the Element::Element(const AbstractContainer&) constructor Element e = mc; return 0; }
Not during initialisation. To use the conversion operator, you'll need to split the assignment into two statements:
Element e;
e = (Element)mc;Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
-
Not during initialisation. To use the conversion operator, you'll need to split the assignment into two statements:
Element e;
e = (Element)mc;Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
Ryan, the problem is that the only code can be modified is the MyContainer class. Assume that all other classes and the main() function represent legacy code. Serge
-
Ryan, the problem is that the only code can be modified is the MyContainer class. Assume that all other classes and the main() function represent legacy code. Serge
Then you can't do it.
Ryan
"Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"