Copy Constructors and reference objects

Started by
10 comments, last by Rattenhirn 11 years ago

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)?

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!

http://infinityelephant.wordpress.com

Advertisement

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.

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.

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?

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!

http://infinityelephant.wordpress.com

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.

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).

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

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

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

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.

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

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.

This topic is closed to new replies.

Advertisement