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. Puzzle about c++ inherent

Puzzle about c++ inherent

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studio
4 Posts 4 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
    sharion
    wrote on last edited by
    #1

    Here is the code snippet.

    class Super
    {
    public:
    Super(){}
    virtual void someMethod() {cout<<"in Super class"<<endl;}
    };

    class Sub : public Super
    {
    public:
    Sub();
    virtual void someMethod(){cout<<"in Sub class"<<endl;}
    };

    Sub mySub;
    Super& ref =mySub;
    ref.someMethod();

    I don't really understand how the compiler works when it comes to the statement ref.someMethod(); . I guess the compiler first find someMethod() method in the Sub class. If doesn't find, then enter base class---Super class to find it. above explanation is correct? ------------ If altering someMethod() in Sub class like this: virtual void someMethod(int i){cout<<"in Sub class"<<endl;} what happens when the compiler runs ref.someMethod(); statement? Obviously the output is "in Super class". ------------ If the someMethod() in Sub class is virtual void someMethod(int i = 1){cout<<"in Sub class"<<endl} when the compiler runs ref.someMethod(); statement,what is the output? In VS 2005,the output is "in Super class". I feel it is hard to understand,I believe the right result is "in Sub class". Anyone could give me some idea,thanks in advance. sharion

    _ P S 3 Replies Last reply
    0
    • S sharion

      Here is the code snippet.

      class Super
      {
      public:
      Super(){}
      virtual void someMethod() {cout<<"in Super class"<<endl;}
      };

      class Sub : public Super
      {
      public:
      Sub();
      virtual void someMethod(){cout<<"in Sub class"<<endl;}
      };

      Sub mySub;
      Super& ref =mySub;
      ref.someMethod();

      I don't really understand how the compiler works when it comes to the statement ref.someMethod(); . I guess the compiler first find someMethod() method in the Sub class. If doesn't find, then enter base class---Super class to find it. above explanation is correct? ------------ If altering someMethod() in Sub class like this: virtual void someMethod(int i){cout<<"in Sub class"<<endl;} what happens when the compiler runs ref.someMethod(); statement? Obviously the output is "in Super class". ------------ If the someMethod() in Sub class is virtual void someMethod(int i = 1){cout<<"in Sub class"<<endl} when the compiler runs ref.someMethod(); statement,what is the output? In VS 2005,the output is "in Super class". I feel it is hard to understand,I believe the right result is "in Sub class". Anyone could give me some idea,thanks in advance. sharion

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      This feature is called run-time linking. Based on the current type of the object the function call is resolved. So in first case the output is printed as "in Sub class" ie when function signatures in super and sub are same When the function signatures are different, and the call is with a int parameter, it would look for a match which is defined in super and function is only available from the base part. So it calls the base class function So obviously the compiler would say, I can not see it and I dont know what to call for and calls the base class exact match function for the remaining two cases.

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      modified on Thursday, April 16, 2009 1:34 AM

      1 Reply Last reply
      0
      • S sharion

        Here is the code snippet.

        class Super
        {
        public:
        Super(){}
        virtual void someMethod() {cout<<"in Super class"<<endl;}
        };

        class Sub : public Super
        {
        public:
        Sub();
        virtual void someMethod(){cout<<"in Sub class"<<endl;}
        };

        Sub mySub;
        Super& ref =mySub;
        ref.someMethod();

        I don't really understand how the compiler works when it comes to the statement ref.someMethod(); . I guess the compiler first find someMethod() method in the Sub class. If doesn't find, then enter base class---Super class to find it. above explanation is correct? ------------ If altering someMethod() in Sub class like this: virtual void someMethod(int i){cout<<"in Sub class"<<endl;} what happens when the compiler runs ref.someMethod(); statement? Obviously the output is "in Super class". ------------ If the someMethod() in Sub class is virtual void someMethod(int i = 1){cout<<"in Sub class"<<endl} when the compiler runs ref.someMethod(); statement,what is the output? In VS 2005,the output is "in Super class". I feel it is hard to understand,I believe the right result is "in Sub class". Anyone could give me some idea,thanks in advance. sharion

        P Offline
        P Offline
        ParagPatel
        wrote on last edited by
        #3

        Hi,

        sharion wrote:

        If doesn't find, then enter base class---Super class to find it. above explanation is correct?

        - Here you are littlebit right, but virtual keyword doing magic here,

        sharion wrote:

        when the compiler runs ref.someMethod();

        Basically there is two type of binding 1. Run-time binding (or late binding) - if same function define in parent class with same signature and it virtual, then compiler ignore it for run-time, and run time function will be called using VPTR and VPTABLE. 2. Compile-time binding (or erly binding) - if above is not a case then compiler resolve binding at compile time only.

        Parag Patel Sr. Software Eng, Varaha Systems

        1 Reply Last reply
        0
        • S sharion

          Here is the code snippet.

          class Super
          {
          public:
          Super(){}
          virtual void someMethod() {cout<<"in Super class"<<endl;}
          };

          class Sub : public Super
          {
          public:
          Sub();
          virtual void someMethod(){cout<<"in Sub class"<<endl;}
          };

          Sub mySub;
          Super& ref =mySub;
          ref.someMethod();

          I don't really understand how the compiler works when it comes to the statement ref.someMethod(); . I guess the compiler first find someMethod() method in the Sub class. If doesn't find, then enter base class---Super class to find it. above explanation is correct? ------------ If altering someMethod() in Sub class like this: virtual void someMethod(int i){cout<<"in Sub class"<<endl;} what happens when the compiler runs ref.someMethod(); statement? Obviously the output is "in Super class". ------------ If the someMethod() in Sub class is virtual void someMethod(int i = 1){cout<<"in Sub class"<<endl} when the compiler runs ref.someMethod(); statement,what is the output? In VS 2005,the output is "in Super class". I feel it is hard to understand,I believe the right result is "in Sub class". Anyone could give me some idea,thanks in advance. sharion

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          sharion wrote:

          If the someMethod() in Sub class is virtual void someMethod(int i = 1){cout<<"in Sub class"<<endl} when the compiler runs ref.someMethod(); statement,what is the output? In VS 2005,the output is "in Super class". I feel it is hard to understand,I believe the right result is "in Sub class".

          No, the compiler is correct. In this case, there are two possible functions matching ref.someMethod(). The C++ Standard defines rules by which the compiler can decide which is the best match. These rules specify that a function with no parameters is a better match than a function with a parameter which has a default value. The fact that one of the methods is implemented in the derived class and one in the base class has no impact.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          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