Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Smart Pointers and virtual

Reply
Thread Tools

Smart Pointers and virtual

 
 
mosfet
Guest
Posts: n/a
 
      08-18-2008
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:ocketOutlook::ContactFold er::get_Items':
overriding virtual function return type differs and is not covariant
from 'System::WindowsMobile:ocketOutlook::Folder::get _Items'

I Don't understand the error because ContactCollection is inheriting
from PimItemCollection.








 
Reply With Quote
 
 
 
 
kwikius
Guest
Posts: n/a
 
      08-18-2008
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:ocketOutlook::ContactFold er::get_Items':
> overriding virtual function return type differs and is not covariant
> from 'System::WindowsMobile:ocketOutlook::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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM
Smart Card Certificate Logon and Smart Card Wireless EAP-TLS erha Wireless Networking 0 05-19-2005 01:40 AM
trade 64mb smart media for 16mb smart media cards wjva Digital Photography 1 08-20-2003 08:30 PM



Advertisments