473,804 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Factories and conversion from Base to Derived...

Hi all,

I want to know whether the following is possible. I started out
with an idea called a CreationAbstrac tor. Basically just a
factory function wrapper for all types.

It looks like this:

template <class T>
class CreationAbstrac tor
{
public:
virtual T* create() const = 0;
virtual ~CreationAbstra ctor(){ }

protected:
CreationAbstrac tor(){ }
};

Typically, it would be used as argument to a class constructor like
this:

Driver::Driver( const CreationAbstrac tor<Car>& );

I attempted to make CreationAbstrac tor generic of up to N arguments:

template <class BaseT, class DerivedT = BaseT>
class Creator: public CreationAbstrac tor<BaseT>
{
private:
typedef boost::scoped_p tr<CreationAbst ractor<BaseT ImplT;

ImplT impl_;

struct NoArgImpl: public CreationAbstrac tor<BaseT>
{ virtual BaseT* create() const{ return new DerivedT; } };

template <class Fstruct Impl;

template <class A1>
struct Impl<void(A1)>: public CreationAbstrac tor<BaseT>
{
Impl( A1 a1 ): a1_( a1 ){}
virtual BaseT* create() const{ return new DerivedT( a1_ ); }
A1 a1_;
};

//etc...

public:

virtual BaseT* create() const
{
return impl_->create();
}

Creator(): impl_( new NoArgImpl ){ }
template <class A1>
Creator( A1 a ): impl_( new Impl<void(A1)>( a ) ){ }
//etc..
};

This seems to work fine, although I suppose you guys could
give me some pointers as to how to implement it better. The
problem that I have is that my original implementation had
only on type. Therefore:

template <class T>
class Creator: public CreationAbstrac tor<T>......(1)

This prohibit me from converting Creator<Derived to
CreationAbstrac tor<Base>, despite my best efforts.

Does anybody know of a better implementation using nifty
conversions where a declaration as in (1) may suffice to do?

IOW:

const CreationAbstrac tor<Base>& a = const
CreationAbstrac tor<Derived>&.. .;compiles fine.

Regards,

Werner

Aug 27 '07 #1
2 1730

werasm wrote:

Excuse my fast fingers. See below:
This seems to work fine, although I suppose you guys could
give me some pointers as to how to implement it better. The
problem that I have is that my original implementation had
only on type. Therefore:
only <onetype
>
template <class T>
class Creator: public CreationAbstrac tor<T>......(1)

This prohibit me from converting Creator<Derived to
CreationAbstrac tor<Base>, despite my best efforts.
This prohibits..

Regards again,

W

Aug 27 '07 #2

werasm wrote:
Hi all,

I want to know whether the following is possible. I started out
with an idea called a CreationAbstrac tor. Basically just a
factory function wrapper for all types.

It looks like this:

template <class T>
class CreationAbstrac tor
{
public:
virtual T* create() const = 0;
virtual ~CreationAbstra ctor(){ }

protected:
CreationAbstrac tor(){ }
};

Typically, it would be used as argument to a class constructor like
this:

Driver::Driver( const CreationAbstrac tor<Car>& );
This seems to be a monologue. Anyway, yesterday after writing this
code I contemplated (after forgetting why) why I could not simply have
done

Driver::Driver( std::auto_ptr<C ar);

[Excuse my example... It is the only simple real world case I can
think of now.]

This would have solved the problem. Alternatively I could have used
something
like Loki Factories (I've read the chapter, yes). The reason the
auto_ptr would
not suffice, is that given the following:

Car( std::auto_ptr<E ngine>, std::auto_ptr<I nterior>...), the client
would be
inclined to try this:

Car( new ConcreteEngine( ...), new LeatherInterior (...) );

Which is also frowned upon... not exception safe, so to speak. For
this reason the notion of a factory does not seem like a bad idea.

Loki factories (whilst I have great respect for the ideas) also have
its problems. The book gives to examples - OpNewFactoryUni t
that creates by calling new T(), and PrototypeFactor yUnit, that
uses Clone on prototypes. This implies either default or copy
constructors are required. This also does not cater for the
possibility of implementation classes being dependent on
other classes. There is another issue. Often creator objects
only need to live as long as required. Using prototypes
imply they live forever, and not using prototypes imply
default constructors are required. Another issue that I
contemplated is creating OpAbstractCreat orFactoryUnit. What
I don't like about this, is the fact that sometimes the creator may
have references to temporaries (like strings), and it may not live
longer than required (It ceases to live once the constructors scope
ceases). Storing these as variant to prototypes would imply they
would have to live indefinitely, meaning the creator may not contain
references to temporaries.

Therefore, back to the car example, this would make sense to
me:
Car( const CreationAbstrac tor<Engine>&, const
CreationAbstrac tor<Interior>&. ..)...

This does not burden the implementor with the need to copy construct.
The actual creation is delayed until the actual point of calling,
which would
be in the constructor initializer list - most likely, or in that of a
member, in
which case it would be forwarded. Also, its exception safe, as usually
the
concrete class is created on the stack.

Monologue complete, thank you.

Any implementation tips (or critique) would be welcome.

Kind regards,

Werner

Aug 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2258
by: Robert Ferrell | last post by:
I'm wondering how Python cognoscenti use/implement factory patterns in Python. In my limited C++ experience, one reason to use a factory method is to make it easier to create instances of classes derived off a base class. The instance is declared to be an instance of the base class. The factory returns an instance of a derived class, and everything is groovy. In Python, since type declarations need not be static, it seems there is no...
15
7366
by: G. Peter | last post by:
Hi there, I've a 'funny' error message of my compiler (g++ 2.95.4) that tells me: robot.cpp: In method `Robot::Robot()': robot.cpp:19: warning: deprecated conversion from string constant to `char *' In Line 19 of my file 'robot.cpp' I declare the constructor of class robot like:
5
1484
by: pw4getter | last post by:
Is there any rationale to prohibit implicit conversion from derived** to base** ? To illustrate my question I submit couple lines below: class base { public: base(){} virtual ~base(){} }; class derived: public base
2
1945
by: CD | last post by:
Is this possible: class base; class derived; //:public base vector <base*> bList; vector<derived*> dList; //add some derived class pointer entries to dList;
5
2126
by: Jeff Greenberg | last post by:
Not an experienced c++ programmer here and I've gotten myself a bit stuck. I'm trying to implement a class lib and I've run into a sticky problem that I can't solve. I'd appreciate any help that I can get! Consider 3 classes in the following heirarchy: base / \ deriv1 deriv2 \
9
2380
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New Derived(), New Derived(), New Derived()}
3
1862
by: shuisheng | last post by:
Dear All, Such as I have a template class Base<intwhich can automaticaaly convert to base<douoble>. Base<int <= Base<double> And also I have a derived class Derived<intwhich can automatically convert to Derived<double>.
5
1545
by: Scott M. | last post by:
Why will this fail: (short) txtQty.Text and this succeed? Convert.ToInt16(txtQty.Text)
3
2167
by: jared.grubb | last post by:
Can a private Base class method convert to/from a Derived* using static_cast? The CPL book says that a Derived method is allowed to convert to/from Base, but says nothing about whether a Base method may do the same thing. My embedded compiler and g++ disagree on whether this is legal. Example: class Base { public: void foo(); }; class Derived : private Base { public: void foo(); };
0
9593
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9169
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7633
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.