Original Post
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?