• Create Account

## Copy Constructors and reference objects

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Plethora  Members

687
Like
0Likes
Like

Posted 02 April 2013 - 08:59 AM

My basic question here is:  Is the following code doing what I think its doing?


class Thing
{
private:
std::vector<int> numbers;

public:
Thing(const Thing& thing2)
{
numbers = thing2.numbers;
}
}

class Some_Other_Class
}
private:
Thing &second;

public:
Some_Other_Class(Thing &thisThing)
:   second(thisThing);
{
}
}

int main()
{
Thing first;
Some_Other_Class(first);
}


Sorry if this is overly complex but I wanted to replicate the structure of things in my program to make sure I'm asking the right question.  Long story short, I want to have a reference to a "Thing" object that is passed into another (temporary) object.  I don't want a copy, and I think I'm doing that much correctly, however, am I copying "numbers" or does the second instance of Thing contain a reference to "numbers" (which is my intent)?

Edited by Plethora, 02 April 2013 - 10:43 AM.

I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

### #2Brother Bob  Moderators

10104
Like
0Likes
Like

Posted 02 April 2013 - 09:15 AM

Ignoring the multitude of syntax errors, and, in fact, one big semantic error that fundamentally changes the meaning of your program as it is, you probably meant it to do what you expected it to do. Yes, using references like that will make the Some_Object_Class object hold a reference to the original object and no copy is made.

However, the second declaration is, in fact, not a temporary object where you pass the previously defined object named first to its constructor. It is another default constructed object named first, and that is an error both because it is a conflicting redefinition of the name first, and because the type does not have a default constructor.

### #3Rattenhirn  Members

2896
Like
0Likes
Like

Posted 02 April 2013 - 09:26 AM

I'll assume that the "first" method in class "Thing" is the copy constructor (which would be named "Thing" like the class, not "first").

In that case, there will only be one instance of class "Thing", sitting on the stack of "main". "main" will have a reference to it and "Some_Other_Class" will have a reference to it.

Since there's only one "Thing", there's only one "numbers".

I hope that makes sense. Your example sure doesn't!

If all of these is a good idea depends highly on what you want to do with this. In my experience, having references as members is rarely a good idea. In pretty much any case where I've tried it, it got replaced with at least a plain pointer, because that makes it explicit that something potentially dangerous is going on there.

### #4Plethora  Members

687
Like
0Likes
Like

Posted 02 April 2013 - 10:37 AM

However, the second declaration is, in fact, not a temporary object where you pass the previously defined object named first to its constructor. It is another default constructed object named first, and that is an error both because it is a conflicting redefinition of the name first, and because the type does not have a default constructor.

In that case, there will only be one instance of class "Thing", sitting on the stack of "main". "main" will have a reference to it and "Some_Other_Class" will have a reference to it.
Since there's only one "Thing", there's only one "numbers".

These two things seem to contradict unless I'm missing something...

And yes, your assumption is correct, first is supposed to be named Thing (Edited), that's just a mix up.

Perhaps you can advise me on this... I was attempting to make it work this way rather than with pointers primarily because I've heard the advice "Use references when you can and pointers when you have to" quite a few times.  Is that not good advice when it comes to data members?

Edited by Plethora, 02 April 2013 - 10:44 AM.

I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

### #5Brother Bob  Moderators

10104
Like
1Likes
Like

Posted 02 April 2013 - 10:51 AM

Rattenhirn is probably overlooking the mistake you likely made in your code where Some_Other_Class(first); does not create an unnamed temporary object, but a default constructed named object called first. I don't believe that is what you intended, and what Ratthenhirn assumed.

Under the assumption that you intended to create a second object with a reference to the first object, then his explanation is correct.

5832
Like
0Likes
Like

Posted 02 April 2013 - 10:54 AM

Reference members can only be shallow copied which is a big restriction, you probably want to disable copying (EDIT: and assignment) of classes with reference members. With pointers you can create a new object of the appropriate type and copy all the members.

Both pointer and reference members have the problem that if you hold a pointer/reference to an object which has been destroyed it is undefined behaviour (hopefully undefined in the sense it will crash immediately on access, but usually you just get hard to track down bugs).

Edited by Paradigm Shifter, 02 April 2013 - 10:58 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

5832
Like
0Likes
Like

Posted 02 April 2013 - 11:03 AM

Rattenhirn is probably overlooking the mistake you likely made in your code where Some_Other_Class(first); does not create an unnamed temporary object, but a default constructed named object called first. I don't believe that is what you intended, and what Ratthenhirn assumed.

Under the assumption that you intended to create a second object with a reference to the first object, then his explanation is correct.

I've not heard of that before, are you saying

Some_Other_Class(first);

is essentially the same as

Some_Other_Class first;

?

I think what the OP wanted to do was create a Some_Other_Class instance as a copy of first, somthing like

Some_Other_Class myOtherClass(first);

which should be OK despite the non const reference in the copy constructor (is it even a copy constructor with a non-const reference?).

EDIT: Whoops

Edited by Paradigm Shifter, 02 April 2013 - 11:04 AM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #8Brother Bob  Moderators

10104
Like
0Likes
Like

Posted 02 April 2013 - 11:07 AM

Rattenhirn is probably overlooking the mistake you likely made in your code where Some_Other_Class(first); does not create an unnamed temporary object, but a default constructed named object called first. I don't believe that is what you intended, and what Ratthenhirn assumed.

Under the assumption that you intended to create a second object with a reference to the first object, then his explanation is correct.

I've not heard of that before, are you saying

Some_Other_Class(first);

is essentially the same as

Some_Other_Class first;

Yes, that is correct.

5832
Like
0Likes
Like

Posted 02 April 2013 - 11:09 AM

Lulz then, that's not just mad it is BONKERS mad. Why would they implement it like that? Is it a template thing?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

### #10Brother Bob  Moderators

10104
Like
0Likes
Like

Posted 02 April 2013 - 11:25 AM

I don't know why and I cannot dig deeper into the specification at the moment to find something useful on this, but I suppose it has to do with how parentheses are treated, causing a syntactic ambiguity that has to be resolved in one way or another.

But the ambiguity is not very general. For example, if you add two parameters, or turn the single parameter into some expression other than a single symbol name, it can no longer be a declaration of a named object so it does create an unnamed and temporary object. It is also not a very useful expression in itself since the object (assuming the unnamed temporary is actually created) is created and destroyed immediately.

### #11Plethora  Members

687
Like
0Likes
Like

Posted 02 April 2013 - 12:12 PM

Thanks for the help all, I think a new implementation is in order for me, one with fewer reference data members.

I think I'll also think through my examples a bit better next time.  ;)

I'm working on a game!  It's called "Spellbook Tactics".  I'd love it if you checked it out, offered some feedback, etc.  I am very excited about my progress thus far and confident about future progress as well!

### #12Rattenhirn  Members

2896
Like
0Likes
Like

Posted 03 April 2013 - 04:26 AM

Rattenhirn is probably overlooking the mistake you likely made in your code where Some_Other_Class(first); does not create an unnamed temporary object, but a default constructed named object called first. I don't believe that is what you intended, and what Rattenhirn assumed.

Under the assumption that you intended to create a second object with a reference to the first object, then his explanation is correct.

ZOMG, totally missed that one. I learn something new every day!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.