-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
40.9k61 gold badges193 silver badges308 bronze badges
asked Dec 5, 2018 at 15:09
8
  • 1
    What are all those classes? Commented Dec 5, 2018 at 15:10
  • 6
    This is assignment, not construction. Sounds like you could use a good C++ book Commented Dec 5, 2018 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 = Awith A an existing Obj var. Commented Dec 5, 2018 at 15:12
  • Can't you define a copy constructor for someClass and try it by yourself? Commented Dec 5, 2018 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. Commented Dec 5, 2018 at 15:18

2 Answers 2

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

answered Dec 5, 2018 at 15:33
Sign up to request clarification or add additional context in comments.

3 Comments

> KYL3R Thank you so much for your detail answer.
leave a like or accept the answer, please. If you need more details, don't hesitate to comment or extend your question post.
Thank you for information. I accepted the answer. I'm not used to use this site, so thank you.
0

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

answered Dec 5, 2018 at 15:23

3 Comments

Assignment will be invoked, but not necessarily copy assignment.
ah ok, sorry :)
> eerorika Thank you for your answer. You resolved my question!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.