• Advertisement
Sign in to follow this  

Passing temprary variables by reference

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
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);
}
}




Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement