1
class A {};
class B { public: B (A a) {} };

A a;
B b=a;

I read this from http://www.cplusplus.com/doc/tutorial/typecasting/ . It says this is a implicit type conversion. From class A to class B. I want to ask, is this also an example of copy constructor? Thanks.

user658266
  • 2,397
  • 4
  • 20
  • 14
  • What is the cast type that happens here? – Tamer Shlash Mar 16 '11 at 03:06
  • 1
    @Mr.TAMER there is no cast there whatsoever. It's a _conversion_. There's a significant difference between the two: a cast is something you agree upon with the compiler, at compile-time. A conversion is something that happens at runtime. A cast takes a sequence of bits and reinterprets their meaning; the bits don't change the slightest. A conversion is a set of operations that manipulates bits, changing one form into another. – wilhelmtell Mar 16 '11 at 03:16
  • @wilhelmtell : Thanks, I didn't know, Lesson learned :) – Tamer Shlash Mar 16 '11 at 03:20
  • @wilhelmtell: Your comment is not true. A cast is a grammatical construct that calls for an explicit conversion but that conversion is not (necessarily) a bit reinterpretation. E.g. in `double test(int x) { double y = (double)x; return y; }` there is a cast of an integer to a double. The result of the run time conversion called for by the cast is based on the value of the `int` passed in. The bit pattern is highly unlikely to be preserved. – CB Bailey Mar 16 '11 at 10:41

4 Answers4

4

No, it's not a copy constructor. A copy constructor copies one object of one type into another of the same type:

B::B(const B& b)
{
    // ...
}

As a side note, if you need a copy constructor then you also need a destructor and an assignment operator, and probably a swap function.

What B::B(A) is is a conversion function. It's a constructor that allows you to convert an object of type A into an object of type B.

void f(const B& obj);

void g()
{
    A obja;
    B objb = obja;
    f(obja);
}
Community
  • 1
  • 1
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • Weren't they called implicit constructors in the good ol' days? – zneak Mar 16 '11 at 03:01
  • An implicit constructor is any constructor that's not marked `explicit`. – wilhelmtell Mar 16 '11 at 03:03
  • @wilhelmtell I thought it only worked for constructors that only had one arguments _and_ that weren't marked explicit; isn't that the same as a 'conversion function'? – zneak Mar 16 '11 at 03:07
  • @zneak you can have a none-explicit constructor with multiple arguments as an implicit conversion if all but one of the arguments have default values. – wilhelmtell Mar 16 '11 at 03:14
  • Wouldn't you call this one a 'conversion' function too? – zneak Mar 16 '11 at 03:16
  • Well then I really can't tell the difference between an implicit constructor and a conversion function, so I'll just assume they're pretty much the same thing. I was just curious, really. – zneak Mar 16 '11 at 18:19
2

No, A copy constructor has the form

class A
{
public:
  A(const A& in) {...}
}
AndersK
  • 35,813
  • 6
  • 60
  • 86
1

No, a copy constructor is called when you create a new variable from an object. What you have there is two objects of different types.

Sadiq
  • 2,249
  • 2
  • 24
  • 32
0

The line B b = a; implies that a copy constructor is used, as if you had typed B b = B(a); or B b((B(a)));. That is, the compiler will check whether B has an accessible (public) copy constructor - whether user-defined or the default one provided by the compiler. It doesn't mean, though, that the copy constructor has to be actually called, because the language allows compilers to optimize away redundant calls to constructors.

By adding a user-defined copy constructor to B and making it inaccessible, the same code should produce a compiler error:

class A {};
class B { 
public: 
    B (A ) {}
private:
    B (const B&) {} // <- this is the copy constructor
};

A a;
B b=a;

For example, Comeau says:

"ComeauTest.c", line 10: error: "B::B(const B &)" (declared at line 6), required
          for copy that was eliminated, is inaccessible
  B b=a;
      ^
visitor
  • 8,564
  • 2
  • 26
  • 15