Questions regarding moderate level C++

Started by
22 comments, last by snk_kid 19 years, 1 month ago
Isn't it just a typo and supposed to be IResImp<T> ...?

I mean that shouldn't even comile, as the pure virtual IResImp... clone() isn't defined in a derived class [ResImp]?
Advertisement
Quote:Original post by JimPrice
The clone method is:

ResImp<T>* clone() const {return new ResImp<T>(*this); }

and it is called in the copy ctor as:

resource(const resource& r) : res_ptr(r.res_ptr->clone()) {}

When the copy ctor is called, isn't *this a std::auto_ptr<ResImp> (ie r.res_ptr)? In which case, there is no appropriate ctor for ResImp. Does this mean either a ctor for ResImp taking a std::auto_ptr<ResImp> needs to be added, or the resource copy ctor needs to be changed to:

ResImp<T>* clone() const {return new ResImp<T>(this->res); }

Or am I missing something (probably)?


The member selection operator dereferences a pointer before selecting a member, when it's dereferenced it's (depending on the context) of type ResImp<T>& or const ResImp<T>&, that technique is known as virtual constructor idiom

Quote:Original post by Telastyn
Isn't it just a typo and supposed to be IResImp<T> ...?

I mean that shouldn't even comile, as the pure virtual IResImp... clone() isn't defined in a derived class [ResImp]?


It's not a typo, you can do this its called covariant return types and it can only be applied on virtual member functions in C++.

[Edited by - snk_kid on March 9, 2005 4:43:39 AM]
Quote:
The member selection operator dereferences a pointer before selecting a member, when it's dereferenced it's (depending on the context) of type ResImp<T>& or const ResImp<T>&, that technique is known as virtual constructor idiom


So - presumably you're relying on the implicit copy ctor for ResImp (which would make sense, given it's only got a T member)?

Thanks for the help,
Jim.
Quote:Original post by JimPrice
So - presumably you're relying on the implicit copy ctor for ResImp (which would make sense, given it's only got a T member)?


Yep, the default behaviour is appropriate.

This topic is closed to new replies.

Advertisement