Thread: boost::shared_ptr and dynamic binding

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    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?
    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,273
    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
    Ireland
    Posts
    8,446
    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.
    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