Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Why results are not same in different compiler?? (http://www.go4expert.com/forums/results-compiler-t8367/)

asadullah.ansari 21Jan2008 15:06

Why results are not same in different compiler??
 
Code: cpp

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???

shabbir 21Jan2008 17:36

Re: Why results are not same in different compiler??
 
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

asadullah.ansari 21Jan2008 17:57

Re: Why results are not same in different compiler??
 
Quote:

Originally Posted by shabbir
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

Salem 21Jan2008 21:17

Re: Why results are not same in different compiler??
 
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.

oogabooga 22Jan2008 02:44

Re: Why results are not same in different compiler??
 
Quote:

Originally Posted by asadullah.ansari
If It is true then Why g++ is not supporting ??? Can You give me reason???

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.

technosavvy 22Jan2008 14:16

Re: Why results are not same in different compiler??
 
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!!

asadullah.ansari 22Jan2008 19:27

Re: Why results are not same in different compiler??
 
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


All times are GMT +5.5. The time now is 22:54.