Re: polymorphic base class pointers and template classes
On Jun 7, 2:16 am, "Alf P. Steinbach" <al...@start.no> wrote:
* awm129:
I have a need to wrap several different (related) legacy structures in
thier own classes. This seems to scream template classes with the
structure type as a template parameter for the creation of a private
class member of the templated type.
Now, I would also like to use a polymorphic base class pointer with
this family of classes (for use with a factory) as well as include
some common functionallity in a base class. I would also like to
force the inclusion of this templated private member for each derived
class. This seems increadibly simple but I can't seem to quite wr=
ap
my head around how this would work. I want to do something simlar to
this:
//
// base class to implement common functionallity
//
template<class T>
class Base
This would create a family of distinct base classes.
{
virtual T wrappedStruct = 0;
C++ does not support virtual data.
Yep, this was more to emphasize my desire to force derived classes to
have a member of the template type, I realize its not legal C++
void foo();
What's this for?
so, rename foo() (and taht should be public: void foo();) to public:
void writeMessageToDatabase()
}
Missing semicolon.
Did you intend for Base to have only private (inaccessible) members?
no they will be at least protected, I seem to have omitted all
accessibility modifiers
//
// derived classes
//
template<class T>
class D1 : Base<T>
{
T wrappedStruct;
}
int main()
{
// assume Bar is a structure type
Bar s;
// now I want to use a polymorphic pointer to manage these things
Base* ptr = DFactory( s );
Why do you want that?
See below
What is the common functionality for the classes?
The common functionality would be logging, printing, writting to a
database, etc
}
I dont think I can create an "untyped" (eg. without the <Type>) Base
pointer, but then how do I get my polymorphism to work? Does any =
of
this make sense? Any ideas? Thanks!
You could always use multiple inheritance, like
struct Base
{
virtual ~Base() {}
// whatever
};
struct MyLegacyClass: Base, LegacyClass
{
// Whatever
};
Multiple inheritence might be what I need
However, it may be a disservice to you to suggest anything at all, becaus=
e while
you've described roughly a kind of technical solution, you haven't really
described the problem that this solution was meant to solve, in particula=
r what
the object factory is meant to solve (and the object factory seems to the=
reason
for the wrapping, so if there's a better solution than the factory, which=
there
might well be, depending on what the problem is, then ignore the above).
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url:http://alfps.izfree.com=
/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linkin=
g
to it is even better! Thanks in advance!- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Thanks for the response! Here is a bit more background on what I'm
trying to do:
These legacy structures are a bunch of different messages. I'm
reading these off a pipe and they come in randomly. I want to pass
these to a factory to create the correct type of object and then I
want to pass these messages to several different handlers. These
handlers should only process the messages for which they are
interested in and just ignore the others. I had planned to have each
handler inherit from a base handler that knows how to process the
generic message type (type Base in my example above). Each derived
handler would have overloaded processing methods for the message types
that they are interested in. That way I can call Process( Base* s )
on a derived handler and all generic messages are passed through while
certain message types are caught and processed by an overloaded form
of Process() in the derived handler. The only place the messages need
to be handled differently based on type is inside the handlers, I want
to treat them gernerically outside the handlers; thus my desire for a
base class pointer. This may be easier to see with more psudo code,
but I can't post any at the moment. Does this clarify my intents at
all? Thanks again.