473,473 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Q: Casting and using the pointer

Hi,

I have a question about casting and using the casted pointer: Suppose I
have a 'base' class and a 'derived' class (which is derived from 'base').
Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour? Or will I get
there when using 'd1'? I am asking because I have a similiar scenario, where
the casted pointer is used in an if-statement, like this:

if (p2->is_derived () && d2->func_only_present_in_derived_class ())
{
}

thanks
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #1
7 1443
"Jakob Bieling" <ne*****@gmy.net> wrote...
I have a question about casting and using the casted pointer: Suppose I have a 'base' class and a 'derived' class (which is derived from 'base').
Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour?
No.
Or will I get
there when using 'd1'?
Yes.
I am asking because I have a similiar scenario, where
the casted pointer is used in an if-statement, like this:

if (p2->is_derived () && d2->func_only_present_in_derived_class ())
{
}


If the function 'is_derived' doesn't attempt to interpret the pointer
_as__if_ it points to an object of class 'derived', but uses some other
mechanism (that actually works), you should be fine.

I can imagine that these classes should work:

class base {
public:
virtual ~base() {}
virtual bool is_derived() const { return false; }
};

class derived: public base {
public:
bool is_derived() const ( return true; }
bool func_only_present_in_derived_class();
};

However, with all that said, the rule of thumb still stands: never use
C-style casts (like you did in your post). There is no real need in
them. If you are convinced that you cannot solve your problem without
a C-style cast, your approach is incorrect.

Victor
Jul 22 '05 #2
On Sat, 20 Dec 2003 18:57:32 +0100 in comp.lang.c++, "Jakob Bieling"
<ne*****@gmy.net> was alleged to have written:
if (p2->is_derived () && d2->func_only_present_in_derived_class ())


Why not use dynamic_cast<> ?

Jul 22 '05 #3
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OJ0Fb.612029$Fm2.553208@attbi_s04...
"Jakob Bieling" <ne*****@gmy.net> wrote...
I have a question about casting and using the casted pointer: Suppose
I
have a 'base' class and a 'derived' class (which is derived from
'base'). Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour?


No.
Or will I get
there when using 'd1'?


Yes.
I am asking because I have a similiar scenario, where
the casted pointer is used in an if-statement, like this:

if (p2->is_derived () && d2->func_only_present_in_derived_class ())
{
}


If the function 'is_derived' doesn't attempt to interpret the pointer
_as__if_ it points to an object of class 'derived', but uses some other
mechanism (that actually works), you should be fine.

I can imagine that these classes should work:

class base {
public:
virtual ~base() {}
virtual bool is_derived() const { return false; }
};

class derived: public base {
public:
bool is_derived() const ( return true; }
bool func_only_present_in_derived_class();
};


This is pretty much exactly what my class design looks like *g*
However, with all that said, the rule of thumb still stands: never use
C-style casts (like you did in your post). There is no real need in
them. If you are convinced that you cannot solve your problem without
a C-style cast, your approach is incorrect.


Right, typing a C-style cast is just so much easier .. bad habbit, I
know.

Thanks for your help!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #4
"Jakob Bieling" <ne*****@gmy.net> wrote...
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:OJ0Fb.612029$Fm2.553208@attbi_s04...
"Jakob Bieling" <ne*****@gmy.net> wrote...
I have a question about casting and using the casted pointer: Suppose
I
have a 'base' class and a 'derived' class (which is derived from

'base'). Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour?


No.
Or will I get
there when using 'd1'?


Yes.
I am asking because I have a similiar scenario, where
the casted pointer is used in an if-statement, like this:

if (p2->is_derived () && d2->func_only_present_in_derived_class ()) {
}


If the function 'is_derived' doesn't attempt to interpret the pointer
_as__if_ it points to an object of class 'derived', but uses some other
mechanism (that actually works), you should be fine.

I can imagine that these classes should work:

class base {
public:
virtual ~base() {}
virtual bool is_derived() const { return false; }
};

class derived: public base {
public:
bool is_derived() const ( return true; }
bool func_only_present_in_derived_class();
};


This is pretty much exactly what my class design looks like *g*


David Harmon's recommendation to use dynamic_cast is probably just
what you need. Although, it's known that dynamic_cast is needed very
rarely and you have to examine your design twice before deciding that
dynamic_cast is something you cannot live without.
Jul 22 '05 #5
"David Harmon" <so****@netcom.com> wrote in message
news:40****************@news.west.earthlink.net...
On Sat, 20 Dec 2003 18:57:32 +0100 in comp.lang.c++, "Jakob Bieling"
<ne*****@gmy.net> was alleged to have written:
if (p2->is_derived () && d2->func_only_present_in_derived_class ())


Why not use dynamic_cast<> ?

Well, it would require me to enable RTTI. And since I have the
'is_derived' function anyway (different name and context, but basically it
is the same), the same functionality is implemented twice; one thru RTTI and
the other thru my 'is_derived' function. Other than that, you are right, I
could use it as well.

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #6
Victor Bazarov schrieb:

"Jakob Bieling" <ne*****@gmy.net> wrote...
I have a question about casting and using the casted pointer: Suppose

I
have a 'base' class and a 'derived' class (which is derived from 'base').
Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour?


No.


I think you might get in trouble in general, e.g. if you have

class base {
int x[10000];
};
class base1{
int y
};
class derived :public base1,public base{};

In this case d1 really points to the middle of nowhere and this seem as illegal as

int x[7];
int * y=x-1;

any comments?
Klaus
[snip]
Jul 22 '05 #7
sorry, I wanted to make the first element big:
klaus hoffmann schrieb:

Victor Bazarov schrieb:

"Jakob Bieling" <ne*****@gmy.net> wrote...
I have a question about casting and using the casted pointer: Suppose

I
have a 'base' class and a 'derived' class (which is derived from 'base').
Now I have this scenario:

base* p1 = new base ();
base* p2 = new derived ();

derived* d1 = (derived*) p1;
derived* d2 = (derived*) p2;

Does the cast itself already produce undefined behaviour?


No.


I think you might get in trouble in general, e.g. if you have

class base {
int x;
};
class base1{
int y[10000];
};
class derived :public base1,public base{};

In this case d1 really points to the middle of nowhere and this seem as illegal as

int x[7];
int * y=x-1;

any comments?
Klaus
[snip]

Jul 22 '05 #8

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

Similar topics

5
by: Vinodh Kumar | last post by:
I see that casting changes the value of a pointer in case of multiple inheritance.In single inheritance also it is the same know?Isn't it? Vinodh Kumar P
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
16
by: He Shiming | last post by:
Hi, I'm having a little bit of trouble regarding pointer casting in my program. I don't understand why the following two cases produce different results. Case 1: IInterface *pInterface = new...
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...
1
by: rahul8143 | last post by:
hello, In kernel source code there is ip_fragment.c file my question is regarding pointer function and casting for that look at required snippet from that file There is structure defined for...
9
by: Jess | last post by:
Hello, It seems both static_cast and dynamic_cast can cast a base class pointer/reference to a derived class pointer/reference. If so, is there any difference between them? In addition, if I...
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...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
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 : ...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
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...
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,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.