Smart Pointers and virtual

Discussion in 'C++' started by mosfet, Aug 18, 2008.

  1. mosfet

    mosfet Guest

    Hi,

    I am trying to modify existing code to use smart pointers and I get some
    issues with virtual methods :

    class Folder : public Object
    {
    public:
    friend class PimItemCollection;
    friend class ContactCollection;
    friend class TaskCollection;
    friend class AppointmentCollection;

    // Constructor/destructor
    Folder(OutlookSession* pOutlookSession) = 0;
    virtual ~Folder();

    virtual RefPtr<PimItemCollection> get_Items();

    protected:
    ....
    };



    class ContactFolder : public Folder
    {
    public:
    // Constructor/destructor
    ContactFolder(OutlookSession* pOutlookSession);
    ContactFolder();

    virtual ~ContactFolder();

    virtual RefPtr<ContactCollection> get_Items();
    };

    1>c:\testpoom\System.WindowsMobile.PocketOutlook.h(640) : error C2555:
    'System::WindowsMobile::pocketOutlook::ContactFolder::get_Items':
    overriding virtual function return type differs and is not covariant
    from 'System::WindowsMobile::pocketOutlook::Folder::get_Items'

    I Don't understand the error because ContactCollection is inheriting
    from PimItemCollection.
    mosfet, Aug 18, 2008
    #1
    1. Advertising

  2. mosfet

    kwikius Guest

    mosfet wrote:
    > Hi,
    >
    > I am trying to modify existing code to use smart pointers and I get some
    > issues with virtual methods :
    >
    > class Folder : public Object
    > {
    > public:
    > friend class PimItemCollection;
    > friend class ContactCollection;
    > friend class TaskCollection;
    > friend class AppointmentCollection;
    >
    > // Constructor/destructor
    > Folder(OutlookSession* pOutlookSession) = 0;
    > virtual ~Folder();
    >
    > virtual RefPtr<PimItemCollection> get_Items();
    >
    > protected:
    > ...
    > };
    >
    >
    >
    > class ContactFolder : public Folder
    > {
    > public:
    > // Constructor/destructor
    > ContactFolder(OutlookSession* pOutlookSession);
    > ContactFolder();
    >
    > virtual ~ContactFolder();
    >
    > virtual RefPtr<ContactCollection> get_Items();
    > };
    >
    > 1>c:\testpoom\System.WindowsMobile.PocketOutlook.h(640) : error C2555:
    > 'System::WindowsMobile::pocketOutlook::ContactFolder::get_Items':
    > overriding virtual function return type differs and is not covariant
    > from 'System::WindowsMobile::pocketOutlook::Folder::get_Items'
    >
    > I Don't understand the error because ContactCollection is inheriting
    > from PimItemCollection.


    Unfortunately this is a problem with smart pointers in replacing raw
    pointers.

    When overrriding virtual functions one can modify the result type in
    the override to a derived class of the return in the interface class,
    known as covariant return. Unfortunately this doesnt translate if you
    change to smart pointer as smart pointer to derived is not a derived
    class of smart pointer to base (though it is convertible), but is a
    separate class, so there is no covariant mechanism.

    All you can do is change sig to base class, smart pointer return
    expression in body should be automatically converted , but you then may
    have to downcast again at other places in code, if you use derived class
    direct, though myself I have found this rare in practise. Perhaps
    covariant return is a case of being too clever anyway, breaks encapsulation

    regards
    Andy Little
    kwikius, Aug 18, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vijayaraghavan Kalyanapasupathy

    Smart Pointers and "new"

    Vijayaraghavan Kalyanapasupathy, Feb 24, 2004, in forum: C++
    Replies:
    2
    Views:
    378
    Jeff Flinn
    Feb 24, 2004
  2. MotoK
    Replies:
    59
    Views:
    1,804
    Keith Thompson
    Sep 15, 2006
  3. Matthias Kaeppler
    Replies:
    5
    Views:
    444
    Axter
    Sep 10, 2005
  4. n2xssvv g02gfr12930

    Smart pointers and member function pointers

    n2xssvv g02gfr12930, Nov 26, 2005, in forum: C++
    Replies:
    3
    Views:
    467
    n2xssvv g02gfr12930
    Nov 27, 2005
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    669
Loading...

Share This Page