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. Virtual Overloaded Function Question...

Virtual Overloaded Function Question...

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++help
3 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    John Mancini
    wrote on last edited by
    #1

    I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini

    M P 2 Replies Last reply
    0
    • J John Mancini

      I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini

      M Offline
      M Offline
      Mike Dunn
      wrote on last edited by
      #2

      Polymorphism only kicks in if you are accessing your C++ object through a pointer. This is what you need to do: { derived myDerived, *pDerived; int nData = 5; pDerived = &myDerived; pDerived->fn(nData); } ================== The original message was: I was wondering if somebody can explain why the following doesn't work:

      class base
      {
      public:
      virtual void fn(int nData) ( /* Do Something */ };
      virtual void fn(char * pszData) { /* Do Something */ };
      }

      class derived : public base
      {
      public:
      virtual void fn(char * pszData) { /* Do Something */ };
      }

      void MyFunction
      {
      derived myderived;
      int nData = 5;

      myderived.fn(nData);
      }

      The compiler complains about myderived.fn(nData). It says....
      "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...."

      Why doesn't it just use the base class fn(int nData)????
      Am I doing something wrong or is this C++ behaviour?

      Thanks in Advance
      Chris Mancini

      1 Reply Last reply
      0
      • J John Mancini

        I was wondering if somebody can explain why the following doesn't work: class base { public: virtual void fn(int nData) ( /* Do Something */ }; virtual void fn(char * pszData) { /* Do Something */ }; } class derived : public base { public: virtual void fn(char * pszData) { /* Do Something */ }; } void MyFunction { derived myderived; int nData = 5; myderived.fn(nData); } The compiler complains about myderived.fn(nData). It says.... "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...." Why doesn't it just use the base class fn(int nData)???? Am I doing something wrong or is this C++ behaviour? Thanks in Advance Chris Mancini

        P Offline
        P Offline
        Prabhat Tripathi
        wrote on last edited by
        #3

        You have actually overrided the function 'fn' and using it as a overloaded function. Check it out. Regards, Prabhat ================== The original message was: I was wondering if somebody can explain why the following doesn't work:

        class base
        {
        public:
        virtual void fn(int nData) ( /* Do Something */ };
        virtual void fn(char * pszData) { /* Do Something */ };
        }

        class derived : public base
        {
        public:
        virtual void fn(char * pszData) { /* Do Something */ };
        }

        void MyFunction
        {
        derived myderived;
        int nData = 5;

        myderived.fn(nData);
        }

        The compiler complains about myderived.fn(nData). It says....
        "error C2664: 'fn' : cannot convert parameter 1 from 'int' to 'char *'. ...."

        Why doesn't it just use the base class fn(int nData)????
        Am I doing something wrong or is this C++ behaviour?

        Thanks in Advance
        Chris Mancini

        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