• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Plethora

Copy Constructors and reference objects

11 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

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.

1

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
0

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
0

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;

Yes, that is correct.

0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

0

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 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!

0

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  
Followers 0