# Passing temprary variables by reference

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

## 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 on other sites
This code compiles fine for me. I'm using gcc 3.4.4, what version are you using?

##### Share on other sites
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 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 on other sites
Quote:
 Original post by NitagePassing 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 on other sites
Quote:
 Original post by SpaceDudeI 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 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 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.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002163
×