default arguments resolution
-
consider the below code
class ab
{
public:
virtual void foo(int a = 2) = 0;
};
class ba : public ab
{
public:
void foo(int a = 4){std::cout<this will print me 2 i.e base class signature is used so is default arguemts are resolved during complie time or runtime and why is the above doing so { but i was excepting it to be 4 (derived default)}
-
consider the below code
class ab
{
public:
virtual void foo(int a = 2) = 0;
};
class ba : public ab
{
public:
void foo(int a = 4){std::cout<this will print me 2 i.e base class signature is used so is default arguemts are resolved during complie time or runtime and why is the above doing so { but i was excepting it to be 4 (derived default)}
Default arguments are resolved at compile time. (This is by necessity to build the stack correctly prior to the call. Thus, once a virtual function is called through the vtable, the call stack must already be set up.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Default arguments are resolved at compile time. (This is by necessity to build the stack correctly prior to the call. Thus, once a virtual function is called through the vtable, the call stack must already be set up.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
joe, but y is it sets the argument to base function signature arguments..
-
joe, but y is it sets the argument to base function signature arguments..
Because that's all it "knows" about. The use of the virtual function isn't done (or technically "known") until runtime.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
joe, but y is it sets the argument to base function signature arguments..
The C++ standard says this about default arguments (in section 8.3.6 para 10):
10 A virtual function call (10.3) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.
Note the use of static type of the pointer or reference - the static type of your reference is a reference to the base type, so it uses the base type's default parameter value.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p