473,396 Members | 1,836 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,396 software developers and data experts.

c++ downcasting

44
Hi everybody! I´m trying to make this program where i store in a vector different types of derived objects from a single base class. So i declare the vector like this:

vector<BaseClass> vec;

then, assuming that all derived objects are, naturally, base class objects i push a DerivedClass object into the vector:

vec.push_back(new DerivedClass(...));

When i pop one object from the vector, to know what kind of object i´m dealing with, i made the atribute "type" and the respective get method(getType()) in the base class. Then i do the following, wich gives a cast compilation error :

for(int i=0; i<vec.size(); i++){

if(vec[i].getType() == ...)

DerivedClass *obj = dynamic_cast<DerivedClass*> (vec[i]);
}

I´ve tried also the static cast and gives the same result. I´d be very gratefull for any help with this. Thanks for your attention.
Jul 31 '06 #1
6 6124
Banfa
9,065 Expert Mod 8TB
You have a pointer mis-match, you vector is declared

vector<BaseClass> vec;

but in your cast you use

DerivedClass *obj = dynamic_cast<DerivedClass*> (vec[i]);

vec[i] is of type BaseClass and you are trying to cast it to DerivedClass *
Jul 31 '06 #2
zeny
44
You have a pointer mis-match, you vector is declared

vector<BaseClass> vec;

but in your cast you use

DerivedClass *obj = dynamic_cast<DerivedClass*> (vec[i]);

vec[i] is of type BaseClass and you are trying to cast it to DerivedClass *

Yes, but as i said, aren´t the DerivedClass objects also automatically BaseClass objects???
In Java I know that this works perfectly even with arrays. For example:

BaseClass array[] = new BasClass[10];

array[0] = new DerivedClass(...);

DerivedClass obj = (DerivedClass)array[0];

The above code works in Java. How do i do the same in c++?

thanks
Jul 31 '06 #3
Banfa
9,065 Expert Mod 8TB
Yes, but as i said, aren´t the DerivedClass objects also automatically BaseClass objects???
Yes. This is not about the type of object but how you are referencing it.

The DerivedClass objects are automatically BassClass objects, or rather have the functionality of the BaseClass objects and can be treated as such, however there is no circumstance in which an Object is a pointer to an object

This will never work

BaseClass objBC;
DerivedClass * pObjDC;

pObjDC = dynamic_cast<DerivedClass*>(objBC);

because objBC is not a pointer type and pObjDC is, you are trying to cast object into pointer to object.

If you are going to use pointers you need to dereference the pointer in question.

BaseClass objBC;
DerivedClass * pObjDC;

pObjDC = dynamic_cast<DerivedClass*>(&objBC);



You need to get your head round pointers, which are apparently one of the big differences between Java and C++, Java hides them away so you don't need to know about them.
Jul 31 '06 #4
zeny
44
Thank u so much. Now at least the program compiles sucessfully but when i try to access the methods of the pObjDC (pObjDC -> method()) it throws an exception that i haven´t yet managed to catch.
I must confess i´m a novice in c++ and i really have to get more into pointers.
Jul 31 '06 #5
ima
1
hi everybody...

how to limit the data in BYTE data type?
i have tried this:

if (strlen(passphrase)<8)

but it doesnt work and get error"invalid conversion from BYTE to char".actully, all my data is in BYTE.

thanks in advance.

-ima
Aug 1 '06 #6
D_C
293 100+
That question sounds like it should go in it's own thread.

Are you familiar with the sizeof operator? passphrase is probably not a string (char*). Instead, try sizeof(passphrase), it should return how many bites the variable uses.
Aug 1 '06 #7

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

Similar topics

6
by: Stuart Golodetz | last post by:
Hi, I've got a minor casting issue which I need to check about (marked // <--). I was trying to do a static_cast on it (which didn't work, though I'm not sure why this should be the case?) I...
9
by: Simon Elliott | last post by:
If I have a base class and several possible derived classes, and I have a pointer to the base class, what's the best way of determining whether the pointer is pointing to a base class or to a...
2
by: Michael | last post by:
Ok Guys, on the same thread I'm writing a Game engine, which performs collision detection. I want to sepate the collision boundings libaray from the main game engine and load it as a library....
6
by: Rein Petersen | last post by:
Hi Folks, I've accepted that one cannot downcast between types, however I can't find a way to achieve //... class Employee { public string name;
6
by: Fernando Rodríguez | last post by:
Hi, I have a class called UrlFetcher and keep several instances inside an ArrayList. This class has a fecth() method. When I iterate through the list calling the fetch() method, I have to...
4
by: Joe Doyle | last post by:
I'm trying to do this- .... void method1(System.Array arr) { // happen to know all elements in arr are int's // this line throws an InvalidCastException int intArr = (int) arr; }
3
by: Joe | last post by:
I know that I can upcast and /downcast in C#. Can it be done in VB.NET? I haven't seen any documentation on it. TIA, -- Joe VB.NET/C#/ASP.NET/ASP/VB/C++/Web and DB development/VBA...
5
by: Ryan van Roode | last post by:
Hello, An API function gives me an object of class 'Blah'. I have subclassed 'Blah' and added a few methods: class EnhancedBlah extends Blah { function enhancement1() {} function...
9
snowfall
by: snowfall | last post by:
What is downcasting and how to implement it?? Am not getting any tutorial on this... pls help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.