Pointers and references

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Elliott

    #1

    Pointers and references

    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_b loggs(const bloggs& the_bloggs);

    which leads to a lot of code like this:

    bloggs* my_bloggs = find_bloggs("fo o");
    if (my_bloggs)
    {
    do_stuff_with_b loggs(*my_blogg s);
    }

    The dereference at do_stuff_with_b loggs() 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?

    --
    Simon Elliott http://www.ctsn.co.uk
  • Victor Bazarov

    #2
    Re: Pointers and references

    Simon Elliott wrote:
    [color=blue]
    > 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_b loggs(const bloggs& the_bloggs);
    >
    > which leads to a lot of code like this:
    >
    > bloggs* my_bloggs = find_bloggs("fo o");
    > if (my_bloggs)
    > {
    > do_stuff_with_b loggs(*my_blogg s);
    > }
    >
    > The dereference at do_stuff_with_b loggs() 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?
    >[/color]

    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

    Comment

    Working...