Thread: Casting temporary into base class reference

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937

    Casting temporary into base class reference

    Hi,
    On MSVC++, this compiles and runs as expected (even though we really know an acid is never a base):
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct base
    {
        virtual void doit() = 0;
    };
    
    struct acid : public base
    {
        void doit() { cout << "I did it!" << endl; }
    };
    
    void stuff(base & b)
    {
        b.doit();
    }
    
    int main()
    {
        stuff(acid());
        return 0;
    }
    However, on GCC, I get the error:
    Code:
    error: invalid initialization of non-const reference of type ‘base&’ from a temporary of type ‘acid’|
    Then, of course, making it work is just a matter of:
    Code:
    //. . . . . 
    acid a;
    stuff(a);
    //. . . .
    And that compiles fine. Why this disparity? Is there any way to make the top code compile in GCC?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    GCC (and current C++ standard) doesn't accept temporaries for non-const references. Allowing this is VC++ extension.

    What you need to do is make stuff take a const base& and make the doit method const, so you would be able to call it on a const object. Otherwise you simply can't use a temporary.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you set compiler warnings to max (Properties -> C/C++ -> General), the compiler will warn about this.
    If you disallow language extensions (Properties -> C/C++ -> Language), it won't compile.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM
  5. Replies: 4
    Last Post: 12-29-2002, 12:29 AM