boost::shared_ptr and dynamic binding

This is a discussion on boost::shared_ptr and dynamic binding within the C++ Programming forums, part of the General Programming Boards category; It seems I cannot get rid of this. I'm probably starting to look suspiciously annoying. Hope I'm not. Tell me ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412

    boost::shared_ptr and dynamic binding

    It seems I cannot get rid of this. I'm probably starting to look suspiciously annoying. Hope I'm not. Tell me when to stop.

    I want to have my allocated memory managed. Period. And in this quest I've been bouncing on walls.

    My latest, and hopefully last one has to to with boost::shared_ptr apparently not supporting dynamic binding. I need your help figuring out if this is indeed the case:

    Code:
    class CItem {
    public:
        typedef boost::shared_ptr<CItem> CItem_Ptr;
    
        virtual CItem& add(CItem_Ptr);
    }
    
    class CContainer : public CItem {
    public:
        virtual CContainer add(CItem_Ptr);
    
    }
    
    class CWeapon : public CItem { /* ... * / }
    
    int main() {
        
        CItem_Ptr weapon(new CWeapon)
        CItem_Ptr obj(new CContainer);
    
        obj->add(weapon)   // This calls CItem::add. Not CContainer::add
    }
    I'm getting so frustrated. Why isn't dynamic binding working? Is it because shared_ptr doesn't support it?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Does it matter that the 'typedef boost::shared_ptr<CItem> CItem_Ptr;' is in the CItem class definition place? I really don't think I can help beyond that tree I'm barking up.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Forget it... found what the problem was.

    I think I need a good night sleep

    EDIT: It was a stupid mistake in which I had the base defining the function as const and the derived not. Embarrassing.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21