Smart Pointers and virtual

M

mosfet

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.
 
K

kwikius

mosfet said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top