Vladimir Grigoriev
2009-10-05 13:27:04 UTC
I have found a difference in behavior of a Borland C++ compiler and Visual
C++ 2005 EE compiler.
class A
{
public:
A( int i = 0 ): a( i ) {}
A( A &rhs ): a( rhs.a ) {}
virtual ~A(){}
A & operator=( A &rhs ) // without const!
{
if ( this != &rhs )
{
a = rhs.a;
}
return ( *this );
}
virtual std::ostream & out( std::ostream &os ) const
{
os << a;
return ( os );
}
private:
int a;
};
inline std::ostream & operator<<( std::ostream &os, const A &rhs )
{
return ( rhs.out( os ) );
}
A f()
{
return ( A( 5 ) );
}
int _tmain(int argc, _TCHAR* argv[])
{
A a1( 10 );
A a2;
a2 = f();
return 0;
}
For the Borland C++ compliker the compilation is failed as the compiler does
not find a required function for the assignment statement a2 = f();
because the assignment operator has no attribute const for rhs parameter
while Visual C++ compiles the code without any error.
Which compiler is wrong according to the C++ standard?
Vladimir Grigoriev
Vladimir Grigoriev
C++ 2005 EE compiler.
class A
{
public:
A( int i = 0 ): a( i ) {}
A( A &rhs ): a( rhs.a ) {}
virtual ~A(){}
A & operator=( A &rhs ) // without const!
{
if ( this != &rhs )
{
a = rhs.a;
}
return ( *this );
}
virtual std::ostream & out( std::ostream &os ) const
{
os << a;
return ( os );
}
private:
int a;
};
inline std::ostream & operator<<( std::ostream &os, const A &rhs )
{
return ( rhs.out( os ) );
}
A f()
{
return ( A( 5 ) );
}
int _tmain(int argc, _TCHAR* argv[])
{
A a1( 10 );
A a2;
a2 = f();
return 0;
}
For the Borland C++ compliker the compilation is failed as the compiler does
not find a required function for the assignment statement a2 = f();
because the assignment operator has no attribute const for rhs parameter
while Visual C++ compiles the code without any error.
Which compiler is wrong according to the C++ standard?
Vladimir Grigoriev
Vladimir Grigoriev