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

C

chris.kemmerer

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
 
V

Victor Bazarov

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
 
C

chris.kemmerer

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



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






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

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
 
S

shazled

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
 
C

chris.kemmerer

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.
 
S

shazled

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
 
A

Adam Nielsen

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top