Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Name lookup

Name lookup

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Serge Krynine
    wrote on last edited by
    #1

    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; }

    R 1 Reply Last reply
    0
    • S Serge Krynine

      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; }

      R Offline
      R Offline
      Ryan Binns
      wrote on last edited by
      #2

      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"

      S 1 Reply Last reply
      0
      • R Ryan Binns

        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"

        S Offline
        S Offline
        Serge Krynine
        wrote on last edited by
        #3

        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

        R 1 Reply Last reply
        0
        • S Serge Krynine

          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

          R Offline
          R Offline
          Ryan Binns
          wrote on last edited by
          #4

          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"

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups