Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help getting to a derived class template given a pointer to a no-template base class

Reply
Thread Tools

Help getting to a derived class template given a pointer to a no-template base class

 
 
chris.kemmerer@att.net
Guest
Posts: n/a
 
      09-26-2007
I am having a problem with templates and I hope someone here can help.

I am writing a library that accepts data packets, parses them and
saves the information for later use. One member of the packet is an
enumeration that says what type of data the packet contains (int,
char, etc.). I have created classes similar to below. The problem I am
having is in trying to access the derived class given only a pointer
to the base class. I know that the pointer I am reading from the
vector points to the appropriate derived class but I have no way of
knowing it's underlying data type before hand so I can't explicitly
declare a variable of the correct derived class. Any help is
appreciated even if it is a definitive "Can't do it".

Thanks,

Chris

class PacketBase
{
virtual ~PacketBase() {}
...
};

template<typename T>
class Packet : public PacketBase
{
std::vector<T> Values() { return m_Values; }
std::vector<T> m_Values;
...
};

class UsePackets
{
std::vector<PacketBase*> m_Packets;
...
};

.... somewhere in the main code...
UsePackets foo;
foo.m_Packets.push_back(new Packet<int>);
foo.m_Packets.push_back(new Packet<short>);
PacketBase* packet = foo.m_Packets.at(1);
packet->Values(); // can't access this function

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-26-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I am having a problem with templates and I hope someone here can help.


It's not a problem with templates. It's a problem with understanding
(or not understanding) how inheritance works, I'm afraid.

> I am writing a library that accepts data packets, parses them and
> saves the information for later use. One member of the packet is an
> enumeration that says what type of data the packet contains (int,
> char, etc.). I have created classes similar to below.


"Similar"?

> The problem I am
> having is in trying to access the derived class given only a pointer
> to the base class. I know that the pointer I am reading from the
> vector points to the appropriate derived class but I have no way of
> knowing it's underlying data type before hand so I can't explicitly
> declare a variable of the correct derived class. Any help is
> appreciated even if it is a definitive "Can't do it".
>
> Thanks,
>
> Chris
>
> class PacketBase
> {
> virtual ~PacketBase() {}
> ...
> };
>
> template<typename T>
> class Packet : public PacketBase
> {
> std::vector<T> Values() { return m_Values; }


Bad idea to return by value. BTW, is this function declared 'private'
intentionally?

> std::vector<T> m_Values;
> ...
> };
>
> class UsePackets
> {
> std::vector<PacketBase*> m_Packets;
> ...
> };
>
> ... somewhere in the main code...
> UsePackets foo;
> foo.m_Packets.push_back(new Packet<int>);
> foo.m_Packets.push_back(new Packet<short>);
> PacketBase* packet = foo.m_Packets.at(1);
> packet->Values(); // can't access this function


What are you trying to do? 'PacketBase' does not have 'Values'
member. Is that what your compiler is telling you? Well, it is
correct. Or is it telling you that the member is "unaccessible"?

Read the FAQ 5.8 and follow its recommendations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
chris.kemmerer@att.net
Guest
Posts: n/a
 
      09-26-2007
On Sep 26, 4:50 pm, "Victor Bazarov" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > I am having a problem with templates and I hope someone here can help.

>
> It's not a problem with templates. It's a problem with understanding
> (or not understanding) how inheritance works, I'm afraid.
>
> > I am writing a library that accepts data packets, parses them and
> > saves the information for later use. One member of the packet is an
> > enumeration that says what type of data the packet contains (int,
> > char, etc.). I have created classes similar to below.

>
> "Similar"?
>
>
>
> > The problem I am
> > having is in trying to access the derived class given only a pointer
> > to the base class. I know that the pointer I am reading from the
> > vector points to the appropriate derived class but I have no way of
> > knowing it's underlying data type before hand so I can't explicitly
> > declare a variable of the correct derived class. Any help is
> > appreciated even if it is a definitive "Can't do it".

>
> > Thanks,

>
> > Chris

>
> > class PacketBase
> > {
> > virtual ~PacketBase() {}
> > ...
> > };

>
> > template<typename T>
> > class Packet : public PacketBase
> > {
> > std::vector<T> Values() { return m_Values; }

>
> Bad idea to return by value. BTW, is this function declared 'private'
> intentionally?
>
>
>
> > std::vector<T> m_Values;
> > ...
> > };

>
> > class UsePackets
> > {
> > std::vector<PacketBase*> m_Packets;
> > ...
> > };

>
> > ... somewhere in the main code...
> > UsePackets foo;
> > foo.m_Packets.push_back(new Packet<int>);
> > foo.m_Packets.push_back(new Packet<short>);
> > PacketBase* packet = foo.m_Packets.at(1);
> > packet->Values(); // can't access this function

>
> What are you trying to do? 'PacketBase' does not have 'Values'
> member. Is that what your compiler is telling you? Well, it is
> correct. Or is it telling you that the member is "unaccessible"?
>
> Read the FAQ 5.8 and follow its recommendations.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


I'm sorry, I was making up these classes to show the issue with as
little superfluous stuff as possible. The Values() functions is
declared public.

I am currently passing by value but that is not a requirement and I
can easily change it to pass by reference.

I know that PacketBase does not have a Values() function because it
can't, PacketBase doesn't know anything about the template type. I
could do away with all this indirection if UsePackets could hold a
vector of Packet class pointers but I didn't think that was possible
since Packet is a class template and each Packet instance can have a
different type.

Where do I find FAQ 5.8?

Thanks,

Chris

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-26-2007
(E-Mail Removed) wrote:
> [..]
> Where do I find FAQ 5.8?


See http://www.parashift.com/c++-faq-lite/ And "Where to I find
the FAQ for this newsgroup?" should be the first question you
ask when you walk in. Of course, you wouldn't have to do that
if you bothered to read the newsgroup for at least a day before
posting...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
shazled@gmail.com
Guest
Posts: n/a
 
      09-27-2007
On Sep 26, 9:32 pm, (E-Mail Removed) wrote:

>
> class PacketBase
> {
> virtual ~PacketBase() {}
> ...
>
> };
>
> template<typename T>
> class Packet : public PacketBase
> {
> std::vector<T> Values() { return m_Values; }
> std::vector<T> m_Values;
> ...
>
> };
>
> class UsePackets
> {
> std::vector<PacketBase*> m_Packets;
> ...
>
> };
>
> ... somewhere in the main code...
> UsePackets foo;
> foo.m_Packets.push_back(new Packet<int>);
> foo.m_Packets.push_back(new Packet<short>);
> PacketBase* packet = foo.m_Packets.at(1);
> packet->Values(); // can't access this function


Would something like this work?

class PacketBase
{
virtual ~PacketBase() {}
virtual std::vector<boost::any>& Values() const =0;
...

};

template<typename T>
class Packet : public PacketBase
{
std::vector<boost::any>& Values() const { return m_Values; }
std::vector<boost::any> m_Values;
...

};

class UsePackets
{
std::vector<PacketBase*> m_Packets;
...

};

... somewhere in the main code...
UsePackets foo;
foo.m_Packets.push_back(new Packet<int>);
foo.m_Packets.push_back(new Packet<short>);
PacketBase* packet = foo.m_Packets.at(1);
vector<boost::any> woot = packet->Values();

You may also be able to wrap the vector itself in boost::any but I've
never done anything like that.

Saul

 
Reply With Quote
 
chris.kemmerer@att.net
Guest
Posts: n/a
 
      09-27-2007
On Sep 27, 5:36 am, (E-Mail Removed) wrote:
> On Sep 26, 9:32 pm, (E-Mail Removed) wrote:
>
>
>
>
>
>
>
> > class PacketBase
> > {
> > virtual ~PacketBase() {}
> > ...

>
> > };

>
> > template<typename T>
> > class Packet : public PacketBase
> > {
> > std::vector<T> Values() { return m_Values; }
> > std::vector<T> m_Values;
> > ...

>
> > };

>
> > class UsePackets
> > {
> > std::vector<PacketBase*> m_Packets;
> > ...

>
> > };

>
> > ... somewhere in the main code...
> > UsePackets foo;
> > foo.m_Packets.push_back(new Packet<int>);
> > foo.m_Packets.push_back(new Packet<short>);
> > PacketBase* packet = foo.m_Packets.at(1);
> > packet->Values(); // can't access this function

>
> Would something like this work?
>
> class PacketBase
> {
> virtual ~PacketBase() {}
> virtual std::vector<boost::any>& Values() const =0;
> ...
>
> };
>
> template<typename T>
> class Packet : public PacketBase
> {
> std::vector<boost::any>& Values() const { return m_Values; }
> std::vector<boost::any> m_Values;
> ...
>
> };
>
> class UsePackets
> {
> std::vector<PacketBase*> m_Packets;
> ...
>
> };
>
> ... somewhere in the main code...
> UsePackets foo;
> foo.m_Packets.push_back(new Packet<int>);
> foo.m_Packets.push_back(new Packet<short>);
> PacketBase* packet = foo.m_Packets.at(1);
> vector<boost::any> woot = packet->Values();
>
> You may also be able to wrap the vector itself in boost::any but I've
> never done anything like that.
>
> Saul- Hide quoted text -
>
> - Show quoted text -


Thank your that idea. I usually don't like to use non-standard
libraries in order to keep my code as portable, and standards based as
possible but I recently attended some classes where Boost was featured
and it looked like it had some very usefull features.

A related question that may actually solve my first problem. The real
issue is being able to create a type from the enumeration I get from
the data packet. If I could somehow morph that into a usable typename
I could dynamically cast the base pointer to the proper derived
template. Something of the form:
typedef typeid(???).name T;
PacketBase* base = foo.m_Packets.at(1);
Packet<T>* derived = dynamic_cast<Packet<T>*>(base);
The problem is ??? isn't an object, just an enumeration.

Another way is if I can somehow pass the type up from the derived
class template to the base class.

In the mean time I'll look into Boost::Any.

Thank you.

 
Reply With Quote
 
shazled@gmail.com
Guest
Posts: n/a
 
      09-27-2007
On Sep 27, 2:26 pm, (E-Mail Removed) wrote:

>
> A related question that may actually solve my first problem. The real
> issue is being able to create a type from the enumeration I get from
> the data packet. If I could somehow morph that into a usable typename
> I could dynamically cast the base pointer to the proper derived
> template. Something of the form:
> typedef typeid(???).name T;
> PacketBase* base = foo.m_Packets.at(1);
> Packet<T>* derived = dynamic_cast<Packet<T>*>(base);
> The problem is ??? isn't an object, just an enumeration.
>


Sorry, I didn't previous realise that the original problem is actually
that you need to store different types in the same container. Its best
not to throw away the type information at all. One possible way is
using vector<boost::variant<int,short,etc> > to hold the packets along
with boost::static_visitor when you want to process them.

Saul

 
Reply With Quote
 
Adam Nielsen
Guest
Posts: n/a
 
      09-28-2007
> I know that the pointer I am reading from the
> vector points to the appropriate derived class but I have no way of
> knowing it's underlying data type before hand so I can't explicitly
> declare a variable of the correct derived class.


Couldn't you use dynamic_cast?

> UsePackets foo;
> foo.m_Packets.push_back(new Packet<int>);
> foo.m_Packets.push_back(new Packet<short>);
> PacketBase* packet = foo.m_Packets.at(1);
> packet->Values(); // can't access this function


Packet<int> *pi = dynamic_cast< Packet<int>* >(packet);
if (pi) pi->Values();
else {
Packet<short> *ps = dynamic_cast< Packet<short>* >(packet);
if (ps) ps->Values();
}

Not the nicest code and there's probably a far better way, but it is a
quick solution.

Cheers,
Adam.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM
Re: template class derived from non-template base class Matt Graham C++ 0 07-21-2003 09:02 PM



Advertisments