Visual C++ 2003 EE is used.
The program that follows
#include "stdafx.h"
#include <iostream>
#pragma hdrstop
#include <conio.h>
class A {};
class B
{
public:
B() { std::cout << "Inside B()" << std::endl; b = 0; }
B( const A & ) { std::cout << "Inside B( const A & )" << std::endl; b =
1; }
B( const B & ) { std::cout << "Inside B( const B & )" << std::endl; b =
2; }
int get_b() const { return b; }
private:
int b;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b1( a );
std::cout << "b1.b = " << b1.get_b() << std::endl;
B b2 = a;
std::cout << "b2.b = " << b2.get_b() << std::endl;
B b3 = B( a );
std::cout << "b3.b = " << b3.get_b() << std::endl;
return EXIT_SUCCESS;
}
gives the result
Inside B( const A & )
b1.b = 1
Inside B( const A & )
b2.b = 1
Inside B( const A & )
b3.b = 1
Press any key to continue . . .
Is it a bug that b3.b = 1? I guessed that b3.b must be equal to 2.
If the copy constructor will be declared as private then the error
error C2248: 'B::B' : cannot access private member declared in class 'B'
will be issued for the statement
B b3 = B( a );
O'k. However what constructor must be called for the statement B b2 = a; ?
If the keyword explicit will be placed before B( const A & ) the error
error C2440: 'initializing' : cannot convert from 'A' to 'B'
will be issued for the statement
B b2 = a;
Vladimir Grigoriev