Q: Casting and using the pointer



 DEVELOP > c-Plus-Plus > Q: Casting and using the pointer

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Jakob Bieling"
Date: 20 Dec 2003 11:57:32 AM
Object: 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)
.

User: "Victor Bazarov"

Title: Re: Casting and using the pointer 20 Dec 2003 12:43:36 PM
"Jakob Bieling" <netsurf@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
.
User: "Jakob Bieling"

Title: Re: Casting and using the pointer 20 Dec 2003 01:44:36 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OJ0Fb.612029$Fm2.553208@attbi_s04...

"Jakob Bieling" <netsurf@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)
.
User: "Victor Bazarov"

Title: Re: Casting and using the pointer 20 Dec 2003 03:40:04 PM
"Jakob Bieling" <netsurf@gmy.net> wrote...

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:OJ0Fb.612029$Fm2.553208@attbi_s04...

"Jakob Bieling" <netsurf@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.
.


User: "klaus hoffmann"

Title: Re: Casting and using the pointer 21 Dec 2003 11:45:43 AM
Victor Bazarov schrieb:


"Jakob Bieling" <netsurf@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]
.
User: "klaus hoffmann"

Title: Re: Casting and using the pointer 21 Dec 2003 11:51:24 AM
sorry, I wanted to make the first element big:
klaus hoffmann schrieb:


Victor Bazarov schrieb:


"Jakob Bieling" <netsurf@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]

.



User: "David Harmon"

Title: Re: Q: Casting and using the pointer 20 Dec 2003 01:26:19 PM
On Sat, 20 Dec 2003 18:57:32 +0100 in comp.lang.c++, "Jakob Bieling"
<netsurf@gmy.net> was alleged to have written:

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

Why not use dynamic_cast<> ?
.
User: "Jakob Bieling"

Title: Re: Q: Casting and using the pointer 20 Dec 2003 03:53:58 PM
"David Harmon" <source@netcom.com> wrote in message
news:4053a1c1.412113529@news.west.earthlink.net...

On Sat, 20 Dec 2003 18:57:32 +0100 in comp.lang.c++, "Jakob Bieling"
<netsurf@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)
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER