Sign in to follow this  

Returning containers from a method.

This topic is 4077 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

I have a bunch of objects that currently return a containers for a caller to operate on. e.g.
class Foo
{
public:
    const std::vector<int> &GetThings() const;

};
Ignoring for the moment whether I should be returning a copy rather than a const reference (in my real scenario, it's not a vector of int's, but a vector of objects that could be tens of kilobytes), what's "the right" way to do this? The callers of these functions generally just iterate over the items, and don't really care much about what the container implementation is. So having a std::vector<int>& return value seems to expose too much of the innards of the class. Would it be better to have something like
class Foo
{
public:
    typedef std::vector<int> things_container;
    const things_container &GetThings() const;
};
So a caller can do
const Foo::things_container &things = theFoo.GetThings();
... and no longer knows/cares what the container implementation is. Or how about not returning the container, but just the begin/end iterators...
class Foo
{
public:
    typedef std::vector<int>::const_iterator things_iterator;
    thing_iterator GetThingsBegin() const;
    thing_iterator GetThingsEnds() const;
};
Any guidelines either way?

Share this post


Link to post
Share on other sites
Just out of curiosity, what are the 'things', and what do you need to do with them?

It seems the simple fact that you require external access to a container that is private to a class should raise a red flag. In other words, the question isn't so much what is the best way to grant this access, but why it's necessary to grant access in the first place.

Anyway, with some more detail about what you're actually doing, I'm sure the gurus here will be able to suggest an appropriate solution.

Share this post


Link to post
Share on other sites
It's basically the syntax tree for a programming language. So in my example, Foo might represent a class definition, and the "Things" might be the methods on that class (the methods in turn would have a list of parameters, list of local variable declarations, and list of code statements).

A client, given a class definition, might want to ask what the methods are.

A client, given a method, might want to find out what the local variable declarations are.

A client, given a local variable declaration, might want to see if it is an array, and if so, get a list of initialization values for that array.

and so on...

Share this post


Link to post
Share on other sites

This topic is 4077 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.

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