Discussion:
Template class usage
(too old to reply)
Timothy Jewett
2010-04-22 17:53:01 UTC
Permalink
I have a project that has a base class, inherited class and inherited class

class CSession
{
public:
BOOL Method1();
BOOL Method2();
};

class CHtml : public CSession
{
BOOL Method1();
BOOL Method2();
};

class CClient : public CHtml
{
BOOL Method1();
BOOL Method2();
};

I would like to Inherit the CHtml class from a different base class

class CSessionSSL : CSecure
{
BOOL Method1();
BOOL Method2();
};

class CHtmlSSL : public CSessionSSL
{
BOOL Method1();
BOOL Method2();
};

class CClientSSL : public CHtmlSSL
{
BOOL Method1();
BOOL Method2();
};

without redefining all the methods in the CHtml & CClient code inherited
from the new base class. I assume this can be done with templates but I do
not understand how to instantiate the top classes CClient & CClientSSL?

Thanks in advance.
--
Timothy Jewett
***@online.nospam
Victor Bazarov
2010-04-22 19:07:17 UTC
Permalink
Post by Timothy Jewett
I have a project that has a base class, inherited class and inherited class
class CSession
{
BOOL Method1();
BOOL Method2();
Not virtual?
Post by Timothy Jewett
};
class CHtml : public CSession
{
BOOL Method1();
BOOL Method2();
};
class CClient : public CHtml
{
BOOL Method1();
BOOL Method2();
};
I would like to Inherit the CHtml class from a different base class
class CSessionSSL : CSecure
{
BOOL Method1();
BOOL Method2();
};
class CHtmlSSL : public CSessionSSL
{
BOOL Method1();
BOOL Method2();
};
class CClientSSL : public CHtmlSSL
{
BOOL Method1();
BOOL Method2();
};
without redefining all the methods in the CHtml & CClient code inherited
from the new base class. I assume this can be done with templates but I do
not understand how to instantiate the top classes CClient & CClientSSL?
You haven't given us enough to go on, I suspect. Are you looking to
implement 'CClientSSL' by utilizing some 'CClient' functionality but
don't want to rewrite/redefine it? And only redefine some small portion
of the base 'Session' class?

So, if you define your CHtml as a template like this:

template<class Session> class CHtml : public Session
{
};

then you can define your Client classes like this:

class CClient : public CHtml<CSession> ...

and

class CClientSSL : public CHtml<CSessionSSL> ...

. I am not sure what exactly you're gaining here. Get a copy of Andrei
Alexandrescu's "Modern C++ Design", and read about "policy-based
design". That's pretty much what you want, you just need to connect
your "clients" using different "sessions".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Timothy Jewett
2010-04-23 15:07:01 UTC
Permalink
You are correct in what I am doing, which is to just have Client objects
connect to to different base classes.
In the example both CClient and CHtml need to be the same code base so do I
define CClient like this:

template<class CHtml<CSession>> class CClientBase : public CHtml

instantiate:
CClient :public CClientBase< Chtml <CSession> >
&
CClientSSL : public CClientBase< CHtml <CSessionSSL> >

I will follow up on that book, Thanks again.
--
Timothy Jewett
Post by Victor Bazarov
Post by Timothy Jewett
I have a project that has a base class, inherited class and inherited class
class CSession
{
BOOL Method1();
BOOL Method2();
Not virtual?
Post by Timothy Jewett
};
class CHtml : public CSession
{
BOOL Method1();
BOOL Method2();
};
class CClient : public CHtml
{
BOOL Method1();
BOOL Method2();
};
I would like to Inherit the CHtml class from a different base class
class CSessionSSL : CSecure
{
BOOL Method1();
BOOL Method2();
};
class CHtmlSSL : public CSessionSSL
{
BOOL Method1();
BOOL Method2();
};
class CClientSSL : public CHtmlSSL
{
BOOL Method1();
BOOL Method2();
};
without redefining all the methods in the CHtml & CClient code inherited
from the new base class. I assume this can be done with templates but I do
not understand how to instantiate the top classes CClient & CClientSSL?
You haven't given us enough to go on, I suspect. Are you looking to
implement 'CClientSSL' by utilizing some 'CClient' functionality but
don't want to rewrite/redefine it? And only redefine some small portion
of the base 'Session' class?
template<class Session> class CHtml : public Session
{
};
class CClient : public CHtml<CSession> ...
and
class CClientSSL : public CHtml<CSessionSSL> ...
.. I am not sure what exactly you're gaining here. Get a copy of Andrei
Alexandrescu's "Modern C++ Design", and read about "policy-based
design". That's pretty much what you want, you just need to connect
your "clients" using different "sessions".
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
Loading...