Puzzle about c++ inherent
-
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 isvirtual 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 -
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 isvirtual 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. sharionThis 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
-
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 isvirtual 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. sharionHi,
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
-
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 isvirtual 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. sharionsharion 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