473,326 Members | 2,076 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,326 software developers and data experts.

casting issues

1
I have a base class and several derived classes. I create a vector of base class type and populate it with derived class objects. I iterate through the vector and the problem is that i get an error saying base class has no member x, which is true because that function is specific to the derived class. The objects and vector are not dynamically allocated, so I have been unsuccessful in finding a way to downcast to call a specific function from the derived type(all the resources i can find use dynamically allocated memory). I can distinguish the derived class objects, i was wondering if anyone knew the easiest way for me to downcast this, just so i can output information returned from a derived type function.
Sep 10 '06 #1
1 1961
Banfa
9,065 Expert Mod 8TB
You can't, here is the problem assuming this code

[code}
class BaseClass
{
...
};

class DerivedClass
{
public:
int m_OnlyInDerivedClass
};

vector<BaseClass> vec;

DerivedClass dc;

vec.push_back(dc);

int zzz = vec[0].m_OnlyInDerivedClass; // this line fails
[/code]

A vector contins an alloctor function that allocates objects of the type the vector stores to enable it to increase it's size as required. The data is copied into these allocated memory blocks using =.

Note that in the vector the type is BaseClass so the vetor stores classes of base type so when dc is stored in the vector all the information only in the DerivedClass is lost as it is cast back to a BaseClass because the vector does not provide the storage to store the extra data, it allocates a BaseClass. The final assignment line does not work and you can not down cast an element of the vector to DerivedClass because the class no longer contains that data.

The solution

don't create a vector of classes, create a vector of pointer to class

Expand|Select|Wrap|Line Numbers
  1. vector<BaseClass *> vec;
  2.  
  3. DerivedClass *dc = new DerivedClass;
  4.  
  5. vec.push_back(dc);
  6.  
  7. int zzz = dynamic_cast<DerivedClass *>(vec[0])->m_OnlyInDerivedClass;
  8.  
Now because we only store the vector the object being pointed at does not suffer the same loss of data problems because the vector is only storing a pointer to BaseClass and this pointer can be downcast to the correct DerivedClass. However also note that we have to provide the allocation (and deallocation) of the data to store the members of the vector ourselves.
Sep 10 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
4
by: David Rager | last post by:
Howdy, Put briefly, I have an array of chars, which I would like to access in pairs of bytes via casting the array to an array of shorts. I'm trying to be as elegant as possible. Below is a...
35
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
61
by: Ken Allen | last post by:
I am relatively new to .Net, but have been using VB and C/C++ for years. One of the drawbacks with VB6 and earlier was the difficulty in casting a 'record' to a different 'shape' so one could...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.