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. vptr help? [modified]

vptr help? [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 Posts 5 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
    sam_psycho
    wrote on last edited by
    #1

    Please tell me how function call is resolve, how vptr works in this case CODE:

    class base
    {
    public:
    virtual void f1()
    {
    cout<<"base f1";
    }
    virtual void f2()
    {
    cout<<"base f2";
    }
    };
    class d1: public base
    {
    public:
    virtual void f1()
    {
    cout<<"d1's f1";
    }

    virtual void f2()
    {
    cout<<"d1's f2";
    }
    };
    class d2: virtual public base,virtual public d1
    {
    public:

    d2()
    {
      cout<<"d2 class construxtor\\n";
    }
    ~d2()
    {
      cout<<" d2 class destructor\\n";
    }
    

    };

    void main()
    {
    clrscr();
    base* obj= new d2;
    obj->f1();
    obj->f2();
    }

    modified on Sunday, July 12, 2009 2:46 PM

    S 1 Reply Last reply
    0
    • S sam_psycho

      Please tell me how function call is resolve, how vptr works in this case CODE:

      class base
      {
      public:
      virtual void f1()
      {
      cout<<"base f1";
      }
      virtual void f2()
      {
      cout<<"base f2";
      }
      };
      class d1: public base
      {
      public:
      virtual void f1()
      {
      cout<<"d1's f1";
      }

      virtual void f2()
      {
      cout<<"d1's f2";
      }
      };
      class d2: virtual public base,virtual public d1
      {
      public:

      d2()
      {
        cout<<"d2 class construxtor\\n";
      }
      ~d2()
      {
        cout<<" d2 class destructor\\n";
      }
      

      };

      void main()
      {
      clrscr();
      base* obj= new d2;
      obj->f1();
      obj->f2();
      }

      modified on Sunday, July 12, 2009 2:46 PM

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

      vptrs don't come into it - you have no virtual functions, so therefore there will be no vtable. If the compiler was able to construct your base pointer obj (it can't because it inherits from base twice - which one should it upcast through?), there would be no runtime resolution of f1 and f2; they're non-virtual functions, so they will be resolved using the static type of obj, i.e. base. That means that the program (if it could compile) would output base f1base f2

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

      G S 2 Replies Last reply
      0
      • S Stuart Dootson

        vptrs don't come into it - you have no virtual functions, so therefore there will be no vtable. If the compiler was able to construct your base pointer obj (it can't because it inherits from base twice - which one should it upcast through?), there would be no runtime resolution of f1 and f2; they're non-virtual functions, so they will be resolved using the static type of obj, i.e. base. That means that the program (if it could compile) would output base f1base f2

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

        G Offline
        G Offline
        Graham Shanks
        wrote on last edited by
        #3

        Actually the code will (and does) compile. Note that base is inherited virtually (note the use of virtual public base in the declaration of class d2). This ensures that only one version of the base class will be included in d2 However, you are completely correct in that there is no vtable, and the output wil be base f1base f2.

        Graham Librarians rule, Ook!

        S J 2 Replies Last reply
        0
        • S Stuart Dootson

          vptrs don't come into it - you have no virtual functions, so therefore there will be no vtable. If the compiler was able to construct your base pointer obj (it can't because it inherits from base twice - which one should it upcast through?), there would be no runtime resolution of f1 and f2; they're non-virtual functions, so they will be resolved using the static type of obj, i.e. base. That means that the program (if it could compile) would output base f1base f2

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

          S Offline
          S Offline
          sam_psycho
          wrote on last edited by
          #4

          all right I got it...but if I am making f1() and f2() virtual in base class and d1 class, how vptr will work?

          S _ 2 Replies Last reply
          0
          • G Graham Shanks

            Actually the code will (and does) compile. Note that base is inherited virtually (note the use of virtual public base in the declaration of class d2). This ensures that only one version of the base class will be included in d2 However, you are completely correct in that there is no vtable, and the output wil be base f1base f2.

            Graham Librarians rule, Ook!

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

            Better tell that to gcc, then - it gives this warning and error:

            a.cpp:30: warning: direct base ‘base’ inaccessible in ‘d2’ due to ambiguity
            a.cpp: In function ‘int main()’:
            a.cpp:46: error: ‘base’ is an ambiguous base of ‘d2’

            VC++ 2008 agrees

            a.cpp(30) : error C2584: 'd2' : direct base 'base' is inaccessible; already a base of 'd1'
            a.cpp(5) : see declaration of 'base'
            a.cpp(16) : see declaration of 'd1'

            The virtual qualifier needs to be applied to all inheritance instances of the shared base, so you need this code for it to compile (note virtual wherever 'base' is inherited):

            class base
            {
            public:
            void f1()
            {
            cout<<"base f1";
            }
            void f2()
            {
            cout<<"base f2";
            }
            };
            class d1: virtual public base
            {
            public:
            void f1()
            {
            cout<<"d1's f1";
            }

            void f2()
            {
            cout<<"d1's f2";
            }
            };
            class d2: virtual public base, public d1
            {
            public:

            d2()
            {
              cout<<"d2 class construxtor\\n";
            }
            ~d2()
            {
              cout<<" d2 class destructor\\n";
            }
            

            };

            int main()
            {
            base* obj= new d2;
            obj->f1();
            obj->f2();
            }

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

            G 1 Reply Last reply
            0
            • S Stuart Dootson

              Better tell that to gcc, then - it gives this warning and error:

              a.cpp:30: warning: direct base ‘base’ inaccessible in ‘d2’ due to ambiguity
              a.cpp: In function ‘int main()’:
              a.cpp:46: error: ‘base’ is an ambiguous base of ‘d2’

              VC++ 2008 agrees

              a.cpp(30) : error C2584: 'd2' : direct base 'base' is inaccessible; already a base of 'd1'
              a.cpp(5) : see declaration of 'base'
              a.cpp(16) : see declaration of 'd1'

              The virtual qualifier needs to be applied to all inheritance instances of the shared base, so you need this code for it to compile (note virtual wherever 'base' is inherited):

              class base
              {
              public:
              void f1()
              {
              cout<<"base f1";
              }
              void f2()
              {
              cout<<"base f2";
              }
              };
              class d1: virtual public base
              {
              public:
              void f1()
              {
              cout<<"d1's f1";
              }

              void f2()
              {
              cout<<"d1's f2";
              }
              };
              class d2: virtual public base, public d1
              {
              public:

              d2()
              {
                cout<<"d2 class construxtor\\n";
              }
              ~d2()
              {
                cout<<" d2 class destructor\\n";
              }
              

              };

              int main()
              {
              base* obj= new d2;
              obj->f1();
              obj->f2();
              }

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

              G Offline
              G Offline
              Graham Shanks
              wrote on last edited by
              #6

              OK, you caught me. :-O That'll learn me not to trust VS2005 :doh:

              Graham Librarians rule, Ook!

              S 1 Reply Last reply
              0
              • S sam_psycho

                all right I got it...but if I am making f1() and f2() virtual in base class and d1 class, how vptr will work?

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

                As base is declared virtual, there is one instance of base in d2. This means (IIUC) that the functions in d1 will hide the functions in base from the point-of-view of d2 because d1 is more derived than base.

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

                1 Reply Last reply
                0
                • G Graham Shanks

                  OK, you caught me. :-O That'll learn me not to trust VS2005 :doh:

                  Graham Librarians rule, Ook!

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

                  Graham Shanks wrote:

                  VS2005

                  That compiled in VS2005? I guess I can understand why, I think. Maybe. Also yet another example why it's good to keep clear of tricksy inheritance features in C++. a) the programmer isn't 100% sure what should happen (I'm definitely including myself in that group), and b) neither's the compiler.

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

                  J 1 Reply Last reply
                  0
                  • S sam_psycho

                    all right I got it...but if I am making f1() and f2() virtual in base class and d1 class, how vptr will work?

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #9

                    For the working of vptr, allow me to redirect you to one of my articles Polymorphism in C[^]

                    «_Superman_» I love work. It gives me something to do between weekends.

                    1 Reply Last reply
                    0
                    • S Stuart Dootson

                      Graham Shanks wrote:

                      VS2005

                      That compiled in VS2005? I guess I can understand why, I think. Maybe. Also yet another example why it's good to keep clear of tricksy inheritance features in C++. a) the programmer isn't 100% sure what should happen (I'm definitely including myself in that group), and b) neither's the compiler.

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

                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #10

                      Stuart Dootson wrote:

                      That compiled in VS2005?

                      No. Doesn't compile in VS 2005, 2008 or VC++ 6.0.

                      1 Reply Last reply
                      0
                      • G Graham Shanks

                        Actually the code will (and does) compile. Note that base is inherited virtually (note the use of virtual public base in the declaration of class d2). This ensures that only one version of the base class will be included in d2 However, you are completely correct in that there is no vtable, and the output wil be base f1base f2.

                        Graham Librarians rule, Ook!

                        J Offline
                        J Offline
                        Joe Woodbury
                        wrote on last edited by
                        #11

                        The code doesn't compile with VC++ 6.0, VS 2005 and VS 2008. (clrscr() is also undeclared in Visual C++; I vaguely recall it being in Turbo C. Perhaps he has an early version of Turbo C++ and it's not showing this as an error.)

                        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