Passing temprary variables by reference

Started by
6 comments, last by SpaceDude 17 years, 8 months ago
I have a question about C++ in general. Suppose I have a function that takes a parameter which is a class passed by reference, although I guess the same occurs when passing integral data types. Should I be able to pass a temporary variable as a function parameter? Let me illustrate with an example:

class CMyClass
{
public:
    CMyClass(int iNumber) : m_iNumber(iNumber) {}

    int m_iNumber;
};

void PrintMe(const CMyClass &a)
{
    cout << a.m_iNumber << endl;
}

int main()
{
    PrintMe(CMyClass(3));   // Note that CMyClass(3) is a temporary variable being passed by reference
    return 0;
}
This will compile with MSVC++ 7.1 and works fine, however when i try to compile with gcc it won't let me compile telling me that it can't match the parameter CMyClass(3) to the PrintMe() function. I can get around this problem by changing the code to:

    {
        CMyClass Temp(3);
        PrintMe(Temp);
    }
however this adds an extra line in my code and is rather anoying since i need to choose a variable name. Can somebody shed some light as to why gcc doesn't like the above code?
Advertisement
This code compiles fine for me. I'm using gcc 3.4.4, what version are you using?
If it's a const reference then yes passing a temporary is perfectly safe, the temporary won't be destroyed until the statement has finished executing. I'm not sure why GCC won't accept it though, what version are you using and what's the exact error message? (I don't have GCC on this machine to try it out with)
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Passing const references to temporaries is valid standard C++.

MSVC++ 7.1 will accept non-const references for to user defined types (unless you diabled microsoft specific C++ extensions).

You're probably declaring you function to accept a non-const reference.

If you really want to pass a non-const reference to a temporary, you can do it like this:

int main()
{
{
CMyClass temp(3);
PrintMe(temp);
}
}




Quote:Original post by Nitage
Passing const references to temporaries is valid standard C++.

MSVC++ 7.1 will accept non-const references for to user defined types (unless you diabled microsoft specific C++ extensions).

You're probably declaring you function to accept a non-const reference.

If you really want to pass a non-const reference to a temporary, you can do it like this:

int main()
{
{
CMyClass temp(3);
PrintMe(temp);
}
}


Ah! You are exactly right... I had used this type of code in several places, and in a few places i forget to declare the variable as const. I've changed it and now compiles fine under gcc... Thank you for the reply, this is new to me.

I think I'll try look for disabling microsoft specific C++ extensions so that I would have this problem in the future since the code I am writing is meant to be multi-platform.
Quote:Original post by SpaceDude
I think I'll try look for disabling microsoft specific C++ extensions so that I would have this problem in the future since the code I am writing is meant to be multi-platform.


That's a nice idea, but you'll run into trouble when you start including anything that isn't standard (eg. windows.h).
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
You can set the "disable microsoft specific C++ extensions" options on a per translation unit basis. You'll need to enable the extensions for any .cpp file that includes windows.h.

What you'll probably end up with is a large set of multi-platform c++ files, and a small set of win32 specific files that rely on microsoft extensions.
Yes well my project is split into several parts. The core code is compiled to a static library, the main program then links this library with other platform specific libraries. So I am able to compile the core code without the MS C++ extensions. Thank you all for your help, this is the first time I am trying to code something that is multi-platform.

This topic is closed to new replies.

Advertisement