1

Suppose I have a class A. I have defined a copy constructor and an assignment operator overloading function. When I do

Class A; Class B=A;

Then while defining Class B, is the copy constructor invoked or the assignment operator?

Thanks in advance.

EDIT: Sorry, I mentioned wrong code. It should be:

A a; A b=a;

4
  • Have you tried putting a break point in your code to find out? Commented Mar 30, 2014 at 7:36
  • Is Class like class Class{}; or did you mistype class? It is not clear. Commented Mar 30, 2014 at 7:55
  • @user2341104...sorry i should have used another notation...here its class Class A{};...see the edit Commented Mar 30, 2014 at 8:59
  • 1
    Anyway, in both cases the copy constructor will be used. Commented Mar 30, 2014 at 9:36

5 Answers 5

3

IIRC T t = value invokes the copy constructor, you can verify that by outputting a string in the constructors to determine which method is used. IIRC when the declaration and assignment are on the same line, it is not called assignment but initialization.

On the other hand, what you've posted does not make sense, you cannot assign one type to another type, you can only assign to type instances.

EDIT: Even if you have a case of two different types (the context of your question is not clear on this one):

class A {};

class B {
public:
   B(const A& other)  { cout << "copy"; }
   B& operator=(const A& other) { cout << "assign"; }
};

int main() {
   A a;
   B b = a; // copy con
   B b1(a); // same as above
   b = a;   // assign op
}

Even then, when both the "copy constructor" and assignment operators take in another type, the copy constructor will still be invoked rather than the assignment operator.

Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you actually mean something like:

A a;
A b = a;

The copy constructor is invoked. The Standard allows = this special meaning in this usage.

3 Comments

It is due to implicit constructor call. You can do it with any implicit conversion constructor.
@NeilKirk while I haven't searched the Standard to prove there's no precedent, I'd hazard that it's not meaningful to call a copy-constructor "implicit", as implicit/explicit relate to controls on construction from a distinct type. (The terms "implicitly-declared" and "implicitly-defined" are used but unrelated). Good observation that it's also allowed for implicit constructors. Cheers.
You can declare it explicit to prevent this syntax.
1

Create a simple class with both, and debug what function is executed by setting a breakpoint in both. Then youll see, and youll also learn a little bit of debugging.

Comments

1

Let's try it out!

#include <iostream>

class A {
 public:
  A() {
    std::cout << "default constructor\n";
  }
  A(const A& other) {
    std::cout << "copy constructor\n";
  }
  A& operator=(const A& rhs) {
    std::cout << "assignment operator\n";
    return *this;
  }
};

int main(int argc, char** argv) {
  std::cout << "first declaration: ";
  A a;
  std::cout << "second declaration: ";
  A b(a);
  std::cout << "third declaration: ";
  A c = a;
  std::cout << "fourth declaration: ";
  A d;
  std::cout << "copying? ";
  d = a;
  return 0;
}

This prints:

first declaration: default constructor
second declaration: copy constructor
third declaration: copy constructor
fourth declaration: default constructor
copying? assignment operator

Working example here: http://codepad.org/DNCpqK2E

Comments

0

when you are defining a new object with assigning another object into it. It invokes copy constructor. e.g, Copy constructor call:

Class A;
Class B=A;

Assignment operator call:

Class A;
Class B;
B=A;

You can always test this, by writing a "print" statement in both the methods to find out which one is being called.

Hope it helped...

8 Comments

@user2341104: Assuming there is a class named Class, which is default-constructable, copy-constructable and assignable, what is wrong with this code?
Well, it is not very clear what the OP means, if it is actually a class named Class or the actual keyword. Especially using capitalization for the "instance" which is common practice for the type.
@user2341104 apparently, I can... I used the name "Class" for the class as the question contains exactly that. otherwise I wouldn't because it's a bad practice.
@user2341104, this is the code example if that is not clear to yet.. #include<iostream> using namespace std; class Class{ int a; public: Class(){ cout<<"default constructor..\n"; a=10; } Class(int b){ cout<<"second constructor..\n"; a=b; } Class(const Class &A){ cout<<"copy constructor..\n"; a=A.a; } Class &operator=(const Class &A){ cout<<"assignment operator..\n"; a=A.a; return *(this); } }; int main(){ Class A(30); Class B(A); Class C=A; B=C; return 0; } Hope it helped...
Yes it is clear what you mean, it is just not clear what the OP means. I am more prone to assume he misspelled class and is actually asking what happens if you use another type in the initialization statement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.