• 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

C++ pointers, references, and function return values...

5 posts in this topic

I feel like I should understand this stuff by now... =/

 

Anyway, lets say I have a class:

class someClass
{
private:
    std::vector<Thing> myVector;

public:
    std::vector<Thing>& getThings();  //simply returns myVector
};

 

Now, lets say that I have another class which contains an instance of someClass.  It has a method whose goal it is to get myVector, iterate through it and pull out some subset of the Things contained in it based on some criteria.  It wants to take that subset and put references (preferably) to those Things in a new vector which it will, itself, control.  This was my attempt at that (minus a lot of the details), and it definitely isn't storing references:

std::vector<Thing> otherVector; //Assume these are private data members in the class               
someClass myClass;              //HigherClass

void HigherClass::doesSomeThings()
{
    std::vector<Thing> &tempVector = myClass.getThings()
    //tempVector should be the same as myVector in myClass, yes?  Like tempVector is
    //another name for myVector?

    for(unsigned int i = 0; i < tempVector.size(); i++)
    {
        if(***some random condition is met***)
        {
            Thing &myThing = tempVector[i];
            //again, myThing should now be an alternate name for the Thing contained at
            //tempVector[i], no?
            otherVector.push_back(myThing);
        }
    }
}

 

So... where am I going wrong?

 

EDIT:  Fixed Formatting

 

EDIT again:  AAAARRGGGHH  apparently you just can't store references in a container like this... which makes perfect sense for very obvious reasons and I'm just an idiot. Someone tell me that my logic at least is correct if this code was for some mythical language that allowed references to be stored in containers.  :)

Edited by Plethora
0

Share this post


Link to post
Share on other sites

Yeah, you store a reference in a pointer.

Instead of

 

Thing &myThing = tempVector[i];

 

you want

 

Thing* myThingPointer = &tempVector[i];


A pointer stores a memory address. A reference is a memory address. This means myThingPointer will point to whatever is at the memory addres that is tempVector[i] currrently. Becareful: If the vector reconfigures itself, myThingPointer might not point to what you think it is anymore. It will still be pointing at the same address, but the contents of that address won't be the same. It could be anything.

Edited by Azaral
0

Share this post


Link to post
Share on other sites

AAAARRGGGHH apparently you just can't store references in a container like this...

 
Yes, you can, all you need to do is declare that the vector is suppose to store pointers, like this:
 

 

std::vector<Thing*>    myVector;
 

 

Take note of the asterisk. This will store an array of pointers for "Thing" into otherVector. The way you have it set up right now, otherVector is in fact storing an array of "Thing" objects themselves, rather than references to them.

Edited by Ludus
0

Share this post


Link to post
Share on other sites

Give reference (or pointer) to object stored on array is usually a bad idea.

In your case your class can be deleted and destroy all the object contained in the array. All the object that have taken reference to there object will have pointer to destroed memory and are likely to crash.

Another problem can araise if you are more object to the original array.

If the array reach it's capacity will reallocate the memory and move all the objects. Again object that have taken the reference will point to dirt memory.

Even if you remove an object from the original array you will have problem.

When you remove an object from a vector the remaining object on the right are shifted to occupy the remaining hole. In this case if you have given a pointer to and objected that is shifted the pointer now will point to the wrong object or to dirt memory.

Your idea will work only if you never touch the array once you give pointer around.

 

Another aproach is to store pointer.

 

class someClass
{
private:
  std::vector<Thing*> myVector;

public:
  std::vector<Thing*>& getThings(); //simply returns myVector
}; 

But this case is even worse.. I can give the pointer around but who is the owner of the pointer? Who must delete the object once is used?

 

class someClass
{
private:
   std::vector<std::auto_ptr<Thing> > myVector;

public:
   std::vector<std::auto_ptr<Thing> >& getThings(); //simply returns myVector
};
 

auto_ptr solve the problem (shared_ptr if you use the new standard). Smart pointer add refcount of object, so, the last that use the object will automatically delete it.

Is still a bad idea tu use standard smart pointer in game development cause they tend to fragment the memory, a better solution is to use boost::intrusive_ptr

http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/intrusive_ptr.html

That have some requirement on the object but it's fast and cache friendly.|

0

Share this post


Link to post
Share on other sites
1) Putting a std::auto_ptr in a standard library container is non-kosher. If you have access to C++11 you can use std::unique_ptr instead.
2) Not all smart pointers use reference counting. In particular std::auto_ptr doesn't.
1

Share this post


Link to post
Share on other sites

Someone tell me that my logic at least is correct if this code was for some mythical language that allowed references to be stored in containers

It would probably be correct in Java, as it does not have objects but only references-to-objects.

In C++ no, your logic is fundamentally flawed and a major misunderstanding. std::vector<Thing> is a vector containing blobs of type Thing, not a vector containing references to Thing objects. It will never, ever store objects 'by address'.

 

To have references, you must have Thing& somewhere. Actually, I'd look into std::reference_wrapper, which counts as a smart &. Standard references using & are a bit quirky, if memory serves, they can be initialized but not copied therefore they cannot live inside std::vector. Now, I don't know if this restriction has been lifted but I still wouldn't write std::vector<Thing&>.

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