Sign in to follow this  
Plethora

Copy Constructors and reference objects

Recommended Posts

Plethora    687

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

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
Rattenhirn    3114

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.

Share this post


Link to post
Share on other sites
Plethora    687

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

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
Plethora    687

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

Share this post


Link to post
Share on other sites
Rattenhirn    3114

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this