Jump to content
  • Advertisement
Sign in to follow this  
VISQI

Vector & List overhead?

This topic is 2546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys
I was wondering about whether passing/returning vector&list references through functions is efficient. I mean that should i do it as a last resort and always think of alternatives or is it good??

here is an example of what i mean:


class Room
{
public:
friend Room& GetRoom();
Room();
~Room();

void Init();

std::vector<InterPt>& GetInterPts();
std::list<Wall>& GetWalls();
std::list<GridPt>& GetGridPts();

ID3D10Buffer* GetWallsBuffer();

private:
std::vector<InterPt> mIPs;
std::list<Wall> mWalls;
std::list<GridPt> mGrid;
};


Note: i am using those public functions heavily.
Thanks for the help

Share this post


Link to post
Share on other sites
Advertisement
Passing or returning references to things is no more expensive than passing or returning pointers, meaning not expensive at all. Returning non-const references to private member variables is perhaps not a good practice, however -- if you really want to expose the member variables totally like this then make them public. But think about whether the references you're returning could be const. The other thing you can do is return (possibly const) iterators.

Share this post


Link to post
Share on other sites

if you really want to expose the member variables totally like this then make them public.

I really want to highlight this point. THe code you've written has accomplished very little, and you're better off just exposing the members directly.

Share this post


Link to post
Share on other sites
If you want to do it the "nice way" and be OO advocate, this is probably what you're looking for:

[source]//Declaring & defining them at the same time makes the compile able to inline it

const std::vector<InterPt>& GetInterPts() const { return mIPs; }
const std::list<Wall>& GetWalls() const { return mWalls; }
const std::list<GridPt>& GetGridPts() const { return mGrid }

[/source]

If you get errors because you're trying to write to them then you're probably doing something wrong, design-wise.

Note beforehand you'll need to change all your std::vector<xx>::iterator with std::vector<xx>::const_iterator or else it will fail to compile (lists too).

Note: i am using those public functions heavily.



If you really you use it that often, and you're looking for efficiency or the encapsulation is completely broken (i.e. you can't constify them without rewriting half of your code), follow Promit's advise.

Share this post


Link to post
Share on other sites

[source]//Declaring & defining them at the same time makes the compiler able to inline it
[/source]

Just a note on this comment: As long as the definitions are in the headers at all the compiler can still inline the functions.

It is typically considered bad practice to declare and define at the same time as it creates a hefty amount of clutter.
It is better practice to put the definitions in the header file after the class declaration (at which point the inline keyword will be explicitly needed).

A common approach to this, employed by Microsoft for DirectX among others, is to put the definitions into a .inl file and #include that file after the class declaration. This has the advantage of keeping the header file occupied only by the class definition, but with the disadvantage of having to manage more files and bounce back and forth between them.


L. Spiro

Share this post


Link to post
Share on other sites

It is typically considered bad practice to declare and define at the same time as it creates a hefty amount of clutter.
It is better practice to put the definitions in the header file after the class declaration (at which point the inline keyword will be explicitly needed).

Ah yes. I overlooked that because some, included myself, consider the get'ers to be an exception. But as a general rule, yes.


Declaring functions whether in the header or separate files included in the header can significantly increase compilation time, which is something to watch out too.

Share this post


Link to post
Share on other sites
thanks for the replies. I guess i'll just inline & const the functions, and then maybe make another function to return the iterator.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!