473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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<TValues() { 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

Sep 26 '07 #1
6 1691
ch************@att.net 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<TValues() { 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
Sep 26 '07 #2
On Sep 26, 4:50 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
chris.kemme...@att.net 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<TValues() { 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

Sep 26 '07 #3
ch************@att.net 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
Sep 26 '07 #4
On Sep 26, 9:32 pm, chris.kemme...@att.net wrote:
>
class PacketBase
{
virtual ~PacketBase() {}
...

};

template<typename T>
class Packet : public PacketBase
{
std::vector<TValues() { 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::anywoot = packet->Values();

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

Saul

Sep 27 '07 #5
On Sep 27, 2:26 pm, chris.kemme...@att.net 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

Sep 27 '07 #6
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.
Sep 28 '07 #7

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

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
4
by: Orion | last post by:
Hi, This is kind of last minute, I have a day and a half left to figure this out. I'm working on a project using ms-sqlserver. We are creating a ticket sales system, as part of the system, I...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
22
by: Dave Cooke | last post by:
Hi I am very new to C. I am trying to figure out how to initialize a struct in my main program. The struct is declared in anouther header file like this... typedef struct ln { int key; int data;...
9
by: santosh | last post by:
Hello all, I've put together a small program to count the number of characters and 'words' in a text file. The minimum length of a word, (in terms of no. of characters), as well as word...
13
by: ppateel | last post by:
Hi, I am new to c++ and I am converting a c program to c++. I changed malloc call to new and I am getting an exception violation. Here is the relevant piece of code. Compiler vc++ 7.0 (.Net...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
15
by: Jay | last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send text messages to many, many employees via system.timer at a 5 second interval. Basically, I look in a SQL table (queue) to...
47
by: Jo | last post by:
Hi there, I'm Jo and it's the first time I've posted here. I'm in process of creating a database at work and have come a little unstuck.....I'm a bit of a novice and wondered if anyone could...
14
by: pgfdbug | last post by:
First I am learning this as I go so please forgive my ignorance, all is self taught. I was given this as program to run an LED signboard for my fire station. The program is supposed to transmit the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.