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 Functions?

Virtual Functions?

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 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.
  • M Offline
    M Offline
    msr_codeproject
    wrote on last edited by
    #1

    class Base
    {
    public:
    Base() { }
    virtual void foo1() { }
    virtual void foo2() { }

    } ;

    class Derived : public Base
    {
    public:
    Derived() { }
    void foo1() { }
    void foo3() { }
    } ;

    int _tmain(int argc, _TCHAR* argv[])
    {
    Derived * d = (Derived*)new Base() ;
    d->foo3() ;
    return 0;
    }

    In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

    C D C J J 5 Replies Last reply
    0
    • M msr_codeproject

      class Base
      {
      public:
      Base() { }
      virtual void foo1() { }
      virtual void foo2() { }

      } ;

      class Derived : public Base
      {
      public:
      Derived() { }
      void foo1() { }
      void foo3() { }
      } ;

      int _tmain(int argc, _TCHAR* argv[])
      {
      Derived * d = (Derived*)new Base() ;
      d->foo3() ;
      return 0;
      }

      In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      d is initialized with the vtable of Base, that's the reason why Base::foo1 is called. On the other hand it is declared as a pointer to a Derived object, hence the compiler (you fooled) calls the foo3 method. Try

      class Derived : public Base
      {
      int data;
      public:
      Derived() { data = 100;}
      void foo1() { }
      void foo3() { cout << data << endl;}
      } ;

      Too see why fooling the compiler is not a good idea.

      Veni, vidi, vici.

      M 1 Reply Last reply
      0
      • C CPallini

        d is initialized with the vtable of Base, that's the reason why Base::foo1 is called. On the other hand it is declared as a pointer to a Derived object, hence the compiler (you fooled) calls the foo3 method. Try

        class Derived : public Base
        {
        int data;
        public:
        Derived() { data = 100;}
        void foo1() { }
        void foo3() { cout << data << endl;}
        } ;

        Too see why fooling the compiler is not a good idea.

        Veni, vidi, vici.

        M Offline
        M Offline
        msr_codeproject
        wrote on last edited by
        #3

        Hi, I have tried this. I am getting some junk value. Why is this happened? :( Regards msr

        C 1 Reply Last reply
        0
        • M msr_codeproject

          class Base
          {
          public:
          Base() { }
          virtual void foo1() { }
          virtual void foo2() { }

          } ;

          class Derived : public Base
          {
          public:
          Derived() { }
          void foo1() { }
          void foo3() { }
          } ;

          int _tmain(int argc, _TCHAR* argv[])
          {
          Derived * d = (Derived*)new Base() ;
          d->foo3() ;
          return 0;
          }

          In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          msr_codeproject wrote:

          1. How it is possible to call foo3() of derived class.

          Just like you have it. What's the problem? :confused:

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous

          1 Reply Last reply
          0
          • M msr_codeproject

            Hi, I have tried this. I am getting some junk value. Why is this happened? :( Regards msr

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Because there is no instance of Derived, the method reads garbage.

            Veni, vidi, vici.

            1 Reply Last reply
            0
            • M msr_codeproject

              class Base
              {
              public:
              Base() { }
              virtual void foo1() { }
              virtual void foo2() { }

              } ;

              class Derived : public Base
              {
              public:
              Derived() { }
              void foo1() { }
              void foo3() { }
              } ;

              int _tmain(int argc, _TCHAR* argv[])
              {
              Derived * d = (Derived*)new Base() ;
              d->foo3() ;
              return 0;
              }

              In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #6

              OK, this is real simple. You instantiate an instance of the "Base" class and then you fool the compiler into thinking it's a "Derived" class object. Then you called a function that only exists in a "real" instantiation of a "Derived" class object. What you did was fool yourself into expecting something reasonable to happen and you're surprised when it doesn't.

              1 Reply Last reply
              0
              • M msr_codeproject

                class Base
                {
                public:
                Base() { }
                virtual void foo1() { }
                virtual void foo2() { }

                } ;

                class Derived : public Base
                {
                public:
                Derived() { }
                void foo1() { }
                void foo3() { }
                } ;

                int _tmain(int argc, _TCHAR* argv[])
                {
                Derived * d = (Derived*)new Base() ;
                d->foo3() ;
                return 0;
                }

                In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                msr_codeproject wrote:

                I am not able to understand this. Please explain.

                In addition to the other replies... Don't write code like that. It is bad code.

                1 Reply Last reply
                0
                • M msr_codeproject

                  class Base
                  {
                  public:
                  Base() { }
                  virtual void foo1() { }
                  virtual void foo2() { }

                  } ;

                  class Derived : public Base
                  {
                  public:
                  Derived() { }
                  void foo1() { }
                  void foo3() { }
                  } ;

                  int _tmain(int argc, _TCHAR* argv[])
                  {
                  Derived * d = (Derived*)new Base() ;
                  d->foo3() ;
                  return 0;
                  }

                  In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr

                  J Offline
                  J Offline
                  JackDingler
                  wrote on last edited by
                  #8

                  Bad line here below. Don't do that.

                  Derived * d = (Derived*)new Base() ;

                  Try

                  Derived * d = new Derived;

                  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