|
|
Home » U++ Library support » U++ Widgets - General questions or Mixed problems » why no 'Ctrl* Ctrl::Clone() const = 0' (virtual constructor)
|
|
Re: why no 'Ctrl* Ctrl::Clone() const = 0' (virtual constructor) [message #28381 is a reply to message #28377] |
Tue, 31 August 2010 13:45 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
the new Ctrl would go to an Array<Ctrl>::Add(Ctrl* newt);
so it wont disappear..
this clone feature is quite well known from C# and i learned to like it..it enables you to manage your object containers from 'bottom', kind of flexibility.
something like the following woule be a layer to use maybe, but includes changes as well
template<class T>
class Clonable
{
public:
virtual T* Clone() const { return DeepCopyNew<T>(*(T*)this); }
virtual T* PartialClone() const { return new T(); }
};
class ValueC
: public Value, public Clonable<ValueC>
{
public:
ValueC() {}
ValueC(const Value& v) : Value(v) {}
};
template<class T>
T* Clone(const T& c) { return DeepCopyNew(c); }
template<class T>
T* PartialClone(const T&) { return new T(); }
[Updated on: Tue, 31 August 2010 13:47] Report message to a moderator
|
|
|
Re: why no 'Ctrl* Ctrl::Clone() const = 0' (virtual constructor) [message #28653 is a reply to message #28381] |
Thu, 09 September 2010 10:56 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
i tried it with the following approach, it works in MSC9 but crashed in TDMGCC, any idea why? (maybe the brute cast is inapropriate..alignment of vtable wrong or sth..)
//copyable interface, implementing the Copy function, used by PolyDeepCopyNew
template<class T, class B = EmptyClass>
class Copyable : public B
{
public:
virtual ~Copyable() {}
virtual T* Copy() const { return new T(); }
};
//this is a nice helper, meant to be used like i.e. PolyCopying<EditCtrl>
//assigning Copy'ed instances to PolyCopying<Ctrl>* with a cast, as Ctrl is direct base class of EditCtrl
template<class T>
class PolyCopying : public Copyable< PolyCopying<T>, PolyDeepCopyNew<PolyCopying<T>, T> > {};
////
PolyCopying<EditInt> abc;
PolyCopying<Ctrl>* pabc = (PolyCopying<Ctrl>*)abc.Copy();
PolyCopying<Ctrl>* pabc2 = pabc->Copy();
Add(pabc2->HSizePos().TopPos(0,100)); //crashes here
delete pabc2;
thats basicly why i need to have the Copy function in Ctrl..it makes factory cloning easy..
[Updated on: Thu, 09 September 2010 11:02] Report message to a moderator
|
|
|
Re: why no 'Ctrl* Ctrl::Clone() const = 0' (virtual constructor) [message #28654 is a reply to message #28653] |
Thu, 09 September 2010 11:54 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
SOLUTION FOUND: by providing a copy interface with base class info
//this one works by providing additional information about the common base class
#if 1
//copyable interface defining the common base class C, without implementing Copy
template<class C>
class CopyableC
{
public:
virtual ~CopyableC() {}
virtual CopyableC* Copy() const = 0;
virtual const C& GetC() const = 0;
virtual C& GetC() = 0;
operator const C&() const {return GetC(); }
operator C&() {return GetC(); }
};
//provides the implementation of Copy, which is used by PolyDeepNew
//and implements the base class accessors.
//T is the derived type, i.e. EditInt, C is common base class, i.e. Ctrl
//PolyCopyingC<EditInt, Ctrl> a;
//CopyableC<Ctrl>* p = a.Copy();
//p->GetC().SizePos();
template<class T, class C>
class PolyCopyingC : public PolyDeepCopyNew<PolyCopyingC<T,C>, T>, public CopyableC<C>
{
public:
virtual PolyCopyingC* Copy() const { return new PolyCopyingC(); }
virtual const C& GetC() const { return *this; }
virtual C& GetC() { return *this; }
};
#endif
///
PolyCopyingC<EditInt, Ctrl> abc; //provide common base class info
CopyableC<Ctrl>* pabc = abc.Copy();
CopyableC<Ctrl>* pabc2 = pabc->Copy();
pabc->GetC().SizePos();
Add(*pabc);
Add(pabc2->GetC().HSizePos().TopPos(0,100));
|
|
|
Goto Forum:
Current Time: Fri Sep 20 06:06:03 CEST 2024
Total time taken to generate the page: 0.04233 seconds
|
|
|