Pointers and references

S

Simon Elliott

I have an API which returns pointers to objects, and another API which
requires a const reference, similar to:

bloggs* find_bloggs(std::string key);
void do_stuff_with_bloggs(const bloggs& the_bloggs);

which leads to a lot of code like this:

bloggs* my_bloggs = find_bloggs("foo");
if (my_bloggs)
{
do_stuff_with_bloggs(*my_bloggs);
}

The dereference at do_stuff_with_bloggs() seems untidy and makes me
wonder if there's any overhead associated with it. Is there a neater
way of doing this, or am I stuck with it?
 
V

Victor Bazarov

Simon said:
I have an API which returns pointers to objects, and another API which
requires a const reference, similar to:

bloggs* find_bloggs(std::string key);
void do_stuff_with_bloggs(const bloggs& the_bloggs);

which leads to a lot of code like this:

bloggs* my_bloggs = find_bloggs("foo");
if (my_bloggs)
{
do_stuff_with_bloggs(*my_bloggs);
}

The dereference at do_stuff_with_bloggs() seems untidy and makes me
wonder if there's any overhead associated with it. Is there a neater
way of doing this, or am I stuck with it?

Stop worrying. If you have to use the API, you're using it the right way.
There is no overhead (except for your uneasiness). You could improve it
ever slightly if you pass 'key' string to 'find_bloggs' by a const ref.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top