Code: class Base { public: virtual void F(int a = 10) { cout<<"Base : "<<a<<endl; } }; class Derived: public Base { public: void F(int b = 20) { cout<<"Derived : "<<b<<endl; } }; void main() { Base* b = new Derived; b->F(2); } When I run this program in defferent plateform It's behaviour is defferent... On Visual Studion 2005 Output is Derived : 10 On g++ and Sun Solaris Output is Derived : 2 I got One reason for Effective C++ Item no 23 like this "The function is overridden and will call the derived class function but the default value is statically bound and will take the default value of base class That being the case, the justification for this Item becomes quite straightforward: virtual functions are dynamically bound, but default parameter values are statically bound." " Never redefine an inherited default parameter value, because default parameter values are statically bound, while virtual functions � the only functions you should be overriding � are dynamically bound. " If It is true then Why g++ is not supporting ??? Can You give me reason???
You have submitted this as an article and I have moved it to the forum. By now I guess you should be now how G4EF is organized into Articles / forums
I did same as it should be but i dont know how it happen?? Offcourse it's not magic. Due to some mistake it happens
The advice tells you not to do something, for some very specific reasons. Then you go ahead and do it anyway. It's like being told "jumping off a cliff will hurt". So you do it and complain "it hurts". > If It is true then Why g++ is not supporting ??? Can You give me reason??? IMO, your code is broken and each compiler is correct in it's own way. Correct code should produce the same answer on ALL compilers. Since the reason stated amounts to "implementation specific" or "undefined" behaviour, absolutely anything can happen. This includes producing the answer you expect based on your preconceived ideas on what should happen. But that doesn't mean the compiler which matches your ideas is right any more than it makes compilers which differ wrong. If you want repeatable code, you need to avoid any hint of code which falls into the - implementation defined, - unspecified, - undefined categories. Read this for further definition of these terms. Talking of things undefined, main returns int, not void.
Where to even start. Firstly, the code you posted does not even demonstrate the point you're trying to make. The call in main() should be b->F() with no parameter. In g++ this correctly gives 10. With the parameter, it had better give 2 in Visual Studio like it does in g++, because that is what you are passing; you are not requesting the default. Secondly, aaaaaaaaaaaarrrrrrrrrrrrgggggggghhhhhh!!!! Sorry, my brain exploded.
yup..that's what happened.. i tried to run the code in Studio 2003 and Studio 2005 and it is giving Derived : 2 as the output!!
I am very very sorry. I am wrongly putted question. No problem of plateform and tools. Intresting question will be when we will call b->F(); Output will be Derived: 10 not Derived :20 This is because static bound of default assignment