Why results are not same in different compiler??

Discussion in 'C++' started by asadullah.ansari, Jan 21, 2008.

  1. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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???
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     
  3. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA

    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
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     
  5. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    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.
     
  6. technosavvy

    technosavvy New Member

    Joined:
    Jan 2, 2008
    Messages:
    52
    Likes Received:
    0
    Trophy Points:
    0
    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!!
     
  7. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice