On 16 Kwi, 19:34, Chris Uzdavinis <cuz...@[EMAIL PROTECTED]
> wrote:
> On Apr 16, 1:22 am, Marcin Swiderski <sfider.b...@[EMAIL PROTECTED]
> wrote:
>
> > On 15 Kwi, 20:33, suresh <suresh.amritap...@[EMAIL PROTECTED]
> wrote:
>
> > > base b1 = 'x'; //why copy constructor not called here?
>
> > This line calls base(const char) constructor and that is the correct
> > behaviour. This sytanx means that object b1 is initialized with
> > value 'x'. It's equivalent to:
>
> > base b1('x');
>
> It's NOT equivalent, it only seems so most of the time.
>
You learn something every day. I've done a little experiment:
# include <iostream>
class A {
public:
A(int i) {
std::cout << "Conversion from integer\n";
};
private:
A(const A& rhs) {
std::cout << "Copy construction\n";
};
};
int main() {
A a = 1;
return 0;
}
This will fail to compile (under gcc 3.4.4) with
the following error:
cnstr.cpp:9: error: `A::A(const A&)' is private
If you delete 'private:' it will compile, but it won't
use the copy constructor.
Furthermore, I've found an example where the use of each
one of the two initialization notations will give a different
output (also under gcc 3.4.4):
# include <iostream>
class B;
class A {
public:
A() {
std::cout << "Default construction\n";
}
A(const B& b) {
std::cout << "Conversion from B\n";
}
A(const A& rhs) {
std::cout << "Copy construction\n";
}
};
class B {
public:
operator A() {
std::cout << "Conversion to A\n";
return A();
}
};
int main() {
B b;
A a = b;
A c(b);
return 0;
}
This program will output:
Conversion to A
Default construction
Conversion from B
Cheers
Sfider
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|