Thread: ptr_container and pointers

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    ptr_container and pointers

    Hello

    I have a ptr_container:

    Code:
    typedef boost::ptr_vector<some_object> my_type;
    my_type m_vector;
    now I make pointer to it:

    Code:
    my_type *p = &m_vector;
    Now I want to have random access to m_vector by using pointer p:

    Code:
    my_type::size_type st;
    for( st = 0u; st != p->size(); ++st ) {
    //now how to call a function inside some_object ?
           p[st]->some_function();
    }
    How should I do this?
    Thanks for help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I believe operator[] returns a reference for ptr_vector. Since p is a pointer, dereference it first before using the [] operator, then use the . to access the function of the object it returns.
    Code:
    (*p)[st].some_function();
    
    // or
    
    p->operator[](st).some_function();

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's the gist of it. Has nothing to do with ptr_vector specifically, just generally with the fact that overloaded operators apply to the direct type of their arguments, and in the case of p, the direct type is my_type*, which is not what you want.

    The fact that overloaded operators do not apply to pointers (and that it would be unintuitive and error-prone if they did) was one of the main reasons references were introduced to the language.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Hello

    I have one more question:

    In case I have:

    Code:
    class base {
    public:
       virtual void function() {
       //do something
       }
    };
    
    class object : public base {
       virtual void function() {
       //do something
       }
    };
    
    typedef boost::ptr_vector<base> my_type;
    my_type m_vector;
    m_vector.push_back(new object());
    my_type *p = &m_vector;
    
    my_type::size_type st;
    for( st = 0u; st != p->size(); ++st ) {
           object *ptr = static_cast<object *>((*p)[st]);
    }
    How should I cast base pointer to object pointer?
    Is there any website where I can read more about that kind of dereferencing and casting? Im really not good with it.

    Thanks for help again

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why do you insist in having a pointer to the ptr_vector, anyway? It's just inconvenient and distracts from the important part of the problem.

    OK, the rules are:
    1) If you're absolutely, absolutely sure that the object in question is of the derived class, use a static_cast. Actually, Boost has a BOOST_STATIC_DOWNCAST or something like that that does a static_cast in release mode and a checked dynamic_cast in debug mode, which is a better idea.
    2) If you're not sure, do a dynamic_cast and check the result. (A dynamic_cast on references throws bad_cast if the cast is disallowed; on pointers it gives a NULL pointer.)
    3) In general, try to avoid downcasting and prefer virtual functions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    The library I have to work with returns a pointer to ptr_vector instead of a reference. No idea why though.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So dereference it and assign it to a reference. After you've made sure you're not responsible for deleting the thing, that is.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Doesnt that mean copying it?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is no copying involved if you assign it to a reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    What is the right way to assign it to a reference?
    my_type ref = &object; ?

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by l2u View Post
    Code:
    my_type::size_type st;
    for( st = 0u; st != p->size(); ++st ) {
    //now how to call a function inside some_object ?
           p[st]->some_function();
    }
    Use p[st].some_function() instead.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by l2u View Post
    What is the right way to assign it to a reference?
    my_type ref = &object; ?
    No.

    Code:
    my_type &ref = object;

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    630
    Is this case, object is a pointer, right?

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    no.
    Code:
    my_type object;
    my_type &ref = object;
    http://www.cprogramming.com/tutorial/references.html

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you have a pointer, dereference the pointer. You dereference with * not &.

Popular pages Recent additions subscribe to a feed