Q: Casting and using the pointer

J

Jakob Bieling

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
 
V

Victor Bazarov

Jakob Bieling said:
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
 
J

Jakob Bieling

Victor Bazarov said:
Suppose

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!
 
V

Victor Bazarov

Jakob Bieling said:
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.
 
J

Jakob Bieling

David Harmon said:
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
 
K

klaus hoffmann

Victor said:

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]
 
K

klaus hoffmann

sorry, I wanted to make the first element big:
klaus said:
Victor said:

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top