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. What is complier actually doing here ?

What is complier actually doing here ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++databaseagentic-aihelp
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.
  • C Offline
    C Offline
    ComplexLifeForm
    wrote on last edited by
    #1

    Hi All, I have a query related to virtual methods and access specifiers in C++ class. I have a two classes and their definition is as shown below class Base { public: virtual void PrintMethod() { cout << "Base class method called"; } }; class Derived : public Base { private: virtual void PrintMethod() { cout << "Derived class method called"; } }; in main method int main() { Derived der; Base *ptr = &der; ptr->PrintMethod(); ------------ (1) // der.PrintMethod(); ------------ (2) return 0; } When I execute the program the output I get is "Derived class method called" and this confused me a bit. I could not figure out properly why the derived class method was called when it is private in class Derived? My guess is that the public function in class Base is inherited in Derived class and when the call was made the through the pointer the linker could only find the private definition of the function PrintMethod and hence it called it through the pointer which is actually pointing to the Derived class object. If I try to call the function through the derived class object (as shown in 2), I get compilation error saying the private method is not accessible which is true. So overall I was not satisfied with the reasoning I arrived at for the derived class method being called. Can someone give me more insight into what actually is happening here? Thanks and Regards :) :)

    C CPalliniC _ 3 Replies Last reply
    0
    • C ComplexLifeForm

      Hi All, I have a query related to virtual methods and access specifiers in C++ class. I have a two classes and their definition is as shown below class Base { public: virtual void PrintMethod() { cout << "Base class method called"; } }; class Derived : public Base { private: virtual void PrintMethod() { cout << "Derived class method called"; } }; in main method int main() { Derived der; Base *ptr = &der; ptr->PrintMethod(); ------------ (1) // der.PrintMethod(); ------------ (2) return 0; } When I execute the program the output I get is "Derived class method called" and this confused me a bit. I could not figure out properly why the derived class method was called when it is private in class Derived? My guess is that the public function in class Base is inherited in Derived class and when the call was made the through the pointer the linker could only find the private definition of the function PrintMethod and hence it called it through the pointer which is actually pointing to the Derived class object. If I try to call the function through the derived class object (as shown in 2), I get compilation error saying the private method is not accessible which is true. So overall I was not satisfied with the reasoning I arrived at for the derived class method being called. Can someone give me more insight into what actually is happening here? Thanks and Regards :) :)

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      When you have a virtual function, the compiler will create a virtual table (also called vtable) in your class which contains the address of all virtual functions (in this case, the address of the Derived::PrintMethod). At that point, it doesn't make any difference if the derived virtual function is private or not. You are calling the function on a base pointer, and there's no way for the compiler to know if the derived function is public or not (the compiler has no way to know what is the real type of your pointer at compile time). The compiler just access the virtual table and redirect the call to the derived method.

      Cédric Moonen Software developer
      Charting control [v1.5] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • C ComplexLifeForm

        Hi All, I have a query related to virtual methods and access specifiers in C++ class. I have a two classes and their definition is as shown below class Base { public: virtual void PrintMethod() { cout << "Base class method called"; } }; class Derived : public Base { private: virtual void PrintMethod() { cout << "Derived class method called"; } }; in main method int main() { Derived der; Base *ptr = &der; ptr->PrintMethod(); ------------ (1) // der.PrintMethod(); ------------ (2) return 0; } When I execute the program the output I get is "Derived class method called" and this confused me a bit. I could not figure out properly why the derived class method was called when it is private in class Derived? My guess is that the public function in class Base is inherited in Derived class and when the call was made the through the pointer the linker could only find the private definition of the function PrintMethod and hence it called it through the pointer which is actually pointing to the Derived class object. If I try to call the function through the derived class object (as shown in 2), I get compilation error saying the private method is not accessible which is true. So overall I was not satisfied with the reasoning I arrived at for the derived class method being called. Can someone give me more insight into what actually is happening here? Thanks and Regards :) :)

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        The compiler may do just a static check on your function calls, on the other hand, polymorphism is a dynamic feature of the language. :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • C ComplexLifeForm

          Hi All, I have a query related to virtual methods and access specifiers in C++ class. I have a two classes and their definition is as shown below class Base { public: virtual void PrintMethod() { cout << "Base class method called"; } }; class Derived : public Base { private: virtual void PrintMethod() { cout << "Derived class method called"; } }; in main method int main() { Derived der; Base *ptr = &der; ptr->PrintMethod(); ------------ (1) // der.PrintMethod(); ------------ (2) return 0; } When I execute the program the output I get is "Derived class method called" and this confused me a bit. I could not figure out properly why the derived class method was called when it is private in class Derived? My guess is that the public function in class Base is inherited in Derived class and when the call was made the through the pointer the linker could only find the private definition of the function PrintMethod and hence it called it through the pointer which is actually pointing to the Derived class object. If I try to call the function through the derived class object (as shown in 2), I get compilation error saying the private method is not accessible which is true. So overall I was not satisfied with the reasoning I arrived at for the derived class method being called. Can someone give me more insight into what actually is happening here? Thanks and Regards :) :)

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

          I have an article on this here. Polymorphism in C[^]

          «_Superman_»

          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