-2

I'm wondering if copy constructor is called or not. If the answer is "No", I don't know what happens. Could you tell me if you know answer for my question?

void func1(someClassA& obj_a)
{
    SomeClassB obj_b;
    obj_b.someClassA = obj_a;    // this is the part I want to ask you!
    obj_b.parameter  = something;

    func2(obj_b);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
graziegrazie
  • 77
  • 2
  • 8
  • 1
    What are all those classes? – SergeyA Dec 05 '18 at 15:10
  • 6
    This is assignment, not construction. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Dec 05 '18 at 15:10
  • copy constructor is in invoked when you declare an object and you intialize it with already an exiting object like `Obj B = A`with A an existing Obj var. – Blood-HaZaRd Dec 05 '18 at 15:12
  • Can't you define a copy constructor for someClass and try it by yourself? – Pierre Baret Dec 05 '18 at 15:13
  • @PierreBaret Proof by trial is dubious in c++. If you're testing something you aren't sure about, you have to keep the possibility of UB in the back of your mind. – François Andrieux Dec 05 '18 at 15:18
  • @FrançoisAndrieux I mean that would be another (better?) question : is this situation an undefined behavior or not? – Pierre Baret Dec 05 '18 at 15:24
  • Note that snippet implies you have a data member of `SomeClassB` whose name is the same as it's type (`someClassA someClassA;`), which will make it hard to use the *type* `someClassA` within the scope of `SomeClassB`. You should probably change the name of that data member. – Caleth Dec 05 '18 at 16:12
  • >SergeyA Those are sample classes to show what I mean. Sorry for my poor explanation. >NathanOliver Thank you for your answer. I'm not familiar with C++. I'll check it. >Blood-HaZaRd Thank you for your detail answer. I understand it! – graziegrazie Dec 20 '18 at 13:01

2 Answers2

0

No, that is an assignment expression. Copy assignment operator will be called. Assuming obj_b.someClassA is of type someClassA.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

In the line you commented, there is no difference when using someClassA& (reference) or someClassA (copy)

A reference is an alias [1] (another name) for the same variable. So you don't pass a pointer, and you don't copy the variable, but you send the reference-to-the-variable. You can use the reference just like the variable itself, because it IS actually referencing the original variable.

The commented line isn't affected by this at all. However the copy constructor is called when you call:

obj_x = someClassA(obj_a); // here, obj_a is given to the (copy-)constructor of someClassA.

What you have is an assignment like @eerorika said.

You can specify a custom assignment operator operator= and handle the action yourself.


More info about references/aliases and how they differ from pointers:

[1] Reference vs. pointer

KYL3R
  • 3,877
  • 1
  • 12
  • 26