reference return type

closed account (zwA4jE8b)
I have this function in a class
1
2
std::vector<RECT>& snake::snake_getsegs()
{return _segments;}


It is used as a parameter to this function
_LVL->level_shownext(_hDC, _foodeaten, _SNK->snake_getsegs());

this is the syntax of
void level::level_shownext(const HDC& _hDC, int _index, std::vector<RECT> _segments)


Is the level class using a reference to the vector in snake class? I believe it is, but would like to make sure.

thank you.
level_shownext() is working on a copy of the vector returned by snake_getsegs(). Whether something is a copy or a reference is only determined by how the function was declared, not what is passed to it.

You can always test things like this in no time.
Also, are you sure naming methods classname_methodname is a good idea?
Last edited on
No, it's taking a copy. The signature should be:
 
void level::level_shownext(const HDC& _hDC, int _index, const std::vector<RECT> &_segments)
closed account (zwA4jE8b)
@kbw
thanks

@hamsterman
I do not see why it would not be a good idea. Is there a possible syntax conflict?



Is my first function,
1
2
std::vector<RECT>& snake::snake_getsegs()
{return _segments;}
correct?

Last edited on
Including the class name again in the function name seems unnecessarily verbose to me. I have never seen that before. Something like that might not be a big deal for some but would be nonsensical for virtual functions.
kbw wrote:
No, it's taking a copy. The signature should be:


void level::level_shownext(const HDC& _hDC, int _index, const std::vector<RECT> &_segments)


Not necessarily. We don't know what snake::snake_getsegs() is actually returning a reference to. Changing shownext may or may not be a good idea. It's awfully suspicious looking if you ask me.
closed account (zwA4jE8b)
its not suspicious, this is all code for my game that I have written.
the reason I need this is because the snake class has the vector containing all of its segments.
I need the level class to access it because when it randomly generates a new piece of 'food' I do not want it to be placed on top of the snake.

This function is only called after the player 'eats' a piece of food, so even though it is a small vector, I did not want to copy it.

snake::snake_getsegs() is returning a vector of the segments (rectangles) named _segments
Last edited on
It's suspicious because I don't see all of the code, and I am not clairvoyant. I have no idea what _segments actually is since I don't see it's definition and use. You are returning a non-const reference, which in my opinion violates encapsulation. If it is a read-only type of a thing that you want some other class to display, then it should be a const reference. There are also possible threading issues depending on whether your program has multiple threads. It's suspicious for a number of reasons.

In fact, sometimes it is necessary to make a copy. Sometimes it isn't. So nobody can really say for certain whether making a copy is good or bad in this case. The answer to your original question is that a copy was being made. As far as KBW's suggestion that it should not copy, well I don't know about that. Maybe, maybe not.
Last edited on
closed account (zwA4jE8b)
I do see what you mean about breaking encapsulation. I just realized this bug in my game and this was an easy way to fix it. (This is also my first game).

Although I will change it to return a const reference
closed account (zwA4jE8b)
So is this a correct way to accomplish passing a vector from the snake class by reference to a function in the level class.

_LVL and _SNK are pointers to level and snake classes respectively.

function in snakengine.cpp
1
2
const std::vector<RECT>& snake::snake_getsegs()
{return _segments;}



code in gamengine.cpp
 
_LVL->level_shownext(_hDC, _foodeaten, _SNK->snake_getsegs());


function in levelengine.cpp
1
2
3
4
void level::level_shownext(const HDC& _hDC, int _index, const std::vector<RECT>& _segments)
{
...code here
}



gamengine.h includes snakengine.h and levelengine.h


or should the returning function not return by reference and just return the vector?

1
2
const std::vector<RECT> snake::snake_getsegs()
{return _segments;}

Last edited on
That prevents the user from modifying. It appears that _segments is a class attribute. if you return a non-const reference to a class attribute then something else could modify it. If the class can be modified by another thread while the other one is reading it, that can cause other kinds of problems. I'm not saying that your code is wrong. I'm just saying that I don't know enough about all of the pieces to say whether KBW's suggestion is correct or not. I think that your original question was very simple and answered. I was merely suggesting that there is some danger associated with returning references to objects, and that sometimes copying is actually the right thing to do. In this case, maybe a read-only reference is okay too. Good luck!
closed account (zwA4jE8b)
well I am glad you brought up some of those issues, especially about encapsulation. I have learned from this, and even though it is a simple game, I have learned a lot.

Thank you.

p.s. Are there any good sites for posting code/zip files for others to check out.
I have noticed that posting huge amounts of code is not a good idea here.
Last edited on
Topic archived. No new replies allowed.