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

casting problems.

I coded up a nice little base class that I wanted to inherit of anytime I
wanted a class to become part of a linked list. It all worked except from
one problem. The GetNext function returned a pointer to the next object but
this pointer was of the bass calls type and when I cast it the overall class
type the pointer did not change as so was now invalid. The only way I got
round it was to a have a void pointer that all the derived classes set to
their this and returned it in the GetNext function. This works but just
feels like a hack.

What I need to know is....

class a
{
int data;
public:
cool functions.
}

class b : public a
{
int more_data;
public:
more funcs
};

if a have a pointer to 'a' when 'a' is really a 'b' how can I cast it to
'b'. ?

Is going to be a runtime op and not a compile time one?

Jul 22 '05 #1
5 1426

"Richard E Collins" <ri*****@collins1969.fsnet.co.uk> wrote in message
news:c1**********@news7.svr.pol.co.uk...
I coded up a nice little base class that I wanted to inherit of anytime I
wanted a class to become part of a linked list. It all worked except from
one problem. The GetNext function returned a pointer to the next object but
this pointer was of the bass calls type and when I cast it the overall class
type the pointer did not change as so was now invalid. The only way I got
round it was to a have a void pointer that all the derived classes set to
their this and returned it in the GetNext function. This works but just
feels like a hack.

What I need to know is....

class a
{
int data;
public:
cool functions.
}

class b : public a
{
int more_data;
public:
more funcs
};

if a have a pointer to 'a' when 'a' is really a 'b' how can I cast it to
'b'. ?


You mean a is polymorphic. Use dynamic_cast.

#include <iostream>
using namespace std;
class a{
int i;
virtual f(){}
};
class b: public a{
};

int main (){
a* aptr = new b;
b* bptr = dynamic_cast<b*>(aptr);
if (bptr){
std::cout << "Fine downcasting!";
}
else
{
std::cout << "Not fine downcasting!";
}
}

This is a run time operation.

-Sharad
Jul 22 '05 #2
I think I had tried that, but then I randomly tried all the cast operators,
been coding for over ten years but never have stretched my C++ skills.

What's all this bit about? Is it needed to make it work?

#include <iostream>
using namespace std;
Thanks for your help mate.
:-)
"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"Richard E Collins" <ri*****@collins1969.fsnet.co.uk> wrote in message
news:c1**********@news7.svr.pol.co.uk...
I coded up a nice little base class that I wanted to inherit of anytime I wanted a class to become part of a linked list. It all worked except from one problem. The GetNext function returned a pointer to the next object but this pointer was of the bass calls type and when I cast it the overall class type the pointer did not change as so was now invalid. The only way I got round it was to a have a void pointer that all the derived classes set to their this and returned it in the GetNext function. This works but just
feels like a hack.

What I need to know is....

class a
{
int data;
public:
cool functions.
}

class b : public a
{
int more_data;
public:
more funcs
};

if a have a pointer to 'a' when 'a' is really a 'b' how can I cast it to
'b'. ?


You mean a is polymorphic. Use dynamic_cast.

#include <iostream>
using namespace std;
class a{
int i;
virtual f(){}
};
class b: public a{
};

int main (){
a* aptr = new b;
b* bptr = dynamic_cast<b*>(aptr);
if (bptr){
std::cout << "Fine downcasting!";
}
else
{
std::cout << "Not fine downcasting!";
}
}

This is a run time operation.

-Sharad

Jul 22 '05 #3

"Richard E Collins" <ri*****@collins1969.fsnet.co.uk> wrote in message
news:c1**********@newsg3.svr.pol.co.uk...
I think I had tried that, but then I randomly tried all the cast operators,
been coding for over ten years but never have stretched my C++ skills.

What's all this bit about? Is it needed to make it work?

#include <iostream>
using namespace std;


I have used the cout object in the demo code I posted.
Now that is defined in header <iostream> (no .h)
Note that iostream.h is deprecated and non-standard.
So you know why I need to write #include <iostream>.

Also everything in these standard headers is wrapped up inside the std
namespce.
Since I was lazy enough to type std::cout everytime so I wrote
using namespace std :-)

Got your answers ?

-Sharad


Jul 22 '05 #4
Trying it all now, thank you very much. I've just noticed that the compiler
has the runtime type checking turned off which may explain when I tried
dynamic_cast last time it didn't work. Mind you I was shooting in the dark
then so it may of failed for other reasons.

Again, thanks for you help. :-)

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:c1*************@ID-221354.news.uni-berlin.de...

"Richard E Collins" <ri*****@collins1969.fsnet.co.uk> wrote in message
news:c1**********@newsg3.svr.pol.co.uk...
I think I had tried that, but then I randomly tried all the cast operators, been coding for over ten years but never have stretched my C++ skills.

What's all this bit about? Is it needed to make it work?

#include <iostream>
using namespace std;


I have used the cout object in the demo code I posted.
Now that is defined in header <iostream> (no .h)
Note that iostream.h is deprecated and non-standard.
So you know why I need to write #include <iostream>.

Also everything in these standard headers is wrapped up inside the std
namespce.
Since I was lazy enough to type std::cout everytime so I wrote
using namespace std :-)

Got your answers ?

-Sharad

Jul 22 '05 #5

"Richard E Collins" <ri*****@collins1969.fsnet.co.uk> wrote in message
news:c1**********@news7.svr.pol.co.uk...
I coded up a nice little base class that I wanted to inherit of anytime I
wanted a class to become part of a linked list. It all worked except from
one problem. The GetNext function returned a pointer to the next object but this pointer was of the bass calls type and when I cast it the overall class type the pointer did not change as so was now invalid. The only way I got
round it was to a have a void pointer that all the derived classes set to
their this and returned it in the GetNext function. This works but just
feels like a hack.


There is a common way of doing this sort of thing: define a template base
class
parameterized by its own derived class:

in outline:

template <class D>
struct Link
{
D* next;
D* prev;
D* ptr() { return static_cast<D*>(this); }
Link(): next( static_cast<D*>(this) ), prev(static_cast<D*>(this)) {}
void unlink()
{
next->prev = prev;
prev->next = next;
next = prev = ptr();
}
void append(D& p)
{
p.unlink();
p.next = next; p.prev = ptr();
next = next.prev = p;
}
.....
};

struct X : public Link<X>
{
....
};

X a,b;
a.append(b);

There are a few cases where this wont work but none are likely to come up in
practice.
Jul 22 '05 #6

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

Similar topics

15
by: Ying Yang | last post by:
Hi, How to I make pointers of different object types point to each other? <snippet> Node2* link2; Node1* link1; link1 = link2 //error
2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
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...
3
by: Andy Lomax | last post by:
I'm using a library where the supplier has provided base class 'foo', and a reference counting class which wraps 'foo': ------------------- library code ----------------------- template<class T>...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
2
by: MackS | last post by:
In C89 can I safely do the following void function(void *p) { FILE *fp = p; /* ... */ return; }
5
by: JS | last post by:
I give a function a void pointer as an argument. But in the function I would like to treat this argument as an integer (only pointers to integers will be sent to the function) therefor I would like...
8
by: Quinn Kirsch | last post by:
Hi all, the following c# example baffles me. my understanding of the managed code of visual .net is that all casts like this would be safe, but this example seems to contradict this notion. if...
5
by: anders.forsgren | last post by:
This is a common problem with generics, but I hope someone has found the best way of solving it. I have these classes: "Fruit" which is a baseclass, and "Apple" which is derived. Further I have...
101
by: Tinkertim | last post by:
Hi, I have often wondered if casting the return value of malloc() (or friends) actually helps anything, recent threads here suggest that it does not .. so I hope to find out. For instance : ...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.