Skip to main content
gamedev.net
🔒 Locked

Setting an accessor function for a private std::vector variable?

Started by chadsxe 6 replies 950+ views
Original Post
chadsxe
chadsxe
I have a class that has a private std::vector variable. I.E. std::vector m_Squares; "cSquare" is a class. I want to set up an accessor function so the outside world can make use of it. This is what I came up with. std::vector& cBlock::GetSquares(){ return m_Squares; } I basically want to double check if this is correct. I want to be passing the address of my vector correct? Regards Chad
Antheus
Antheus
Quote:
I basically want to double check if this is correct. I want to be passing the address of my vector correct?


No, you'll want to pass reference, which is exactly what you're doing. But references do not equal pointers, and they are not addresses. Most compilers just represent them as such - but don't need to.

In addition, it's common to provide two versions of accessors:
typedef std::vector<cSquare> Squares;Squares & cBlock::GetSquares() {return m_Squares;}const Squares & cBlock::GetSquares() const{return m_Squares;}


This way, you don't run into problems when cBlock is const, but retain const-correctness of m_Squares.
mikeman
mikeman
Quote:

I want to be passing the address of my vector correct?


Yes, if you want to expose the vector, you have to return a reference to it.

However, consider some things:

1)Why make the vector private, since you're going to expose it after all?If you're going to do that, just make it public.
2)Do you really want to expose it? Know that users of this class will probably take the vector and do "stuff" with it, maybe even modifying it. Are you sure this will not bring your class into an invalid state?
3)Can't you have the functionality you want without exposing the vector? For example, if you're exposing it so the user can add Squares, you can do that by adding a cBlock::AddSquare() method.

Other than that, I don't know the specifics of your program so I can't say more. Do spend some time thinking about good OO design, but don't overdo it. If you think that exposing the vector will do you more good than harm, then it's your choise.
rip-off
rip-off
Well, most people would return a const reference, and mark the function as const. Depends on what you are doing.

Unless you want to allow other people to modify the list, in which case you should make it public. In many cases, with some work you can change the way your class is used so that instead of providing public access to private members, you can add the functionality to the class as a member function instead. We would need specific examples of what you are doing to make suggestions in this case.

As an aside, I would ask you to question the use of prefixing your class names with "c". For example, "Square" is a perfectly good name for a class. I don't see the point in adding the visual noise that is "cSquare".
chadsxe
chadsxe
You all have a point. Let me share a little more details. I have a class “cBlock” that is made up of multiple squares. My squares are implemented via a classes called “cSquare”. The vector that I have in cBlock houses cSquare objects. The reason why I originally set up the accessor was to access values of the cSquare objects that are housed in the vector.

From the advice given do you think it would be better to set up my accessor in cBlock kind of like this

int cBlock::GetSquareCenterX(int Index){
return m_Squares[Index].GetCenterX();
}

Regards

Chad
chadsxe
chadsxe
You all have a point. Let me share a little more details. I have a class “cBlock” that is made up of multiple squares. My squares are implemented via a classes called “cSquare”. The vector that I have in cBlock houses cSquare objects. The reason why I originally set up the accessor was to access values of the cSquare objects that are housed in the vector.

From the advice given do you think it would be better to set up my accessor in cBlock kind of like this

int cBlock::GetSquareCenterX(int Index){
return m_Squares[Index].GetCenterX();
}

Regards

Chad
stonemetal
stonemetal
yes but why do you want to access the vector shouldn't block handle anything that needs to be done to the vector.
Zahlman
Zahlman
Quote:
Original post by chadsxe
From the advice given do you think it would be better to set up my accessor in cBlock kind of like this

int cBlock::GetSquareCenterX(int Index){
return m_Squares[Index].GetCenterX();
}

Regards

Chad


This is very often the right idea. The exception is when it leads to writing a large number of similar accessors. In that case, you probably want an operator[] overload instead:

Square& cBlock::operator[](int Index) {	return m_Squares[Index];}const Square& cBlock::operator[](int Index) const {	return m_Squares[Index];}// Now the calling code can do e.g. myBlock[5].centerX().

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.