What is complier actually doing here ?
-
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 :) :)
-
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 :) :)
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++ -
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 :) :)
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] -
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 :) :)
I have an article on this here. Polymorphism in C[^]
«_Superman_»