Cast to derived class?

  • Thread starter christian.pontesegger
  • Start date
C

christian.pontesegger

Hi all,

lately I had a problem where I wanted to cast a basic class to a
derived type, which did not introduce any new members and did not
change any stuff with dynamically allocated memory. I just wanted to
add some methods to the class.

So my question is:
Is it valid C++ to create a basic class and to cast it to a derived
one the way I did in my example below? The example works fine for
MinGW, but I'm not sure if this is ABI dependent.

Example code:

<code>

#include <stdio.h>

class Basic {

public:
Basic(int a, int b) {
a_ = a;
b_ = b;
};

int getA() {
return a_;
};
int getB() {
return b_;
};

private:
int a_, b_;
};

class Derived : Basic {

public:
Derived(int a, int b) : Basic(a, b) {}
;

int multiply() {
return getA() * getB();
};
};


int main() {
Basic *basic = new Basic(2, 4);

Derived *derived = (Derived *)basic;
printf("%d * %d = %d\n", basic->getA(), basic->getB(), derived-
multiply());

delete basic;

return 0;
}

</code>
 
I

Ian Collins

Hi all,

So my question is:
Is it valid C++ to create a basic class and to cast it to a derived
one the way I did in my example below? The example works fine for
MinGW, but I'm not sure if this is ABI dependent.

Example code:
int main() {
Basic *basic = new Basic(2, 4);

Derived *derived = (Derived *)basic;

The fact you had to resort to a C style cast is a strong indicator that
this is a bad idea.

The only safe way to cast from a Basic* is to use dynamic_cast and for
that to work, the Basic* must point to a Derived.
 
C

christian.pontesegger

I see I have to be a bit more specific about my problem:

I was trying to write a simulation model for some quite primitive IC
similar to a USB flash chip.

To abstract the memory I wrote some class which clusters the memory
into blocks and enables for write and read access.

Now my IC allows for 2 completely different approaches to justify
access rights on that very same memory (I know this sounds like a bad
idea, but its not up to me to change that).

So I wanted to have 2 derived classes, which will do the checking for
access conditions. But I do not want to use 2 instances of the memory
as I would need to keep them synchronized.

So I have the following model:

-----------
base memory
-----------
/ \
--------- ---------
derived 1 derived 2
--------- ---------

Where derived 1/2 simply read parts of the memory (through a base
method) and return true/false for some access condition checks.

What I wanted to do is instantiate some "derived 1 memory" and cast it
for some other AC checks to "derived 2"


As far as I can see from your answers I should create a new class
which derives from "derived 1" and "derived 2" instead of my casting
approach.


thanks for your replies!
 
P

paolo.brandoli

I see I have to be a bit more specific about my problem:

I was trying to write a simulation model for some quite primitive IC
similar to a USB flash chip.

To abstract the memory I wrote some class which clusters the memory
into blocks and enables for write and read access.

Now my IC allows for 2 completely different approaches to justify
access rights on that very same memory (I know this sounds like a bad
idea, but its not up to me to change that).

So I wanted to have 2 derived classes, which will do the checking for
access conditions. But I do not want to use 2 instances of the memory
as I would need to keep them synchronized.

So I have the following model:

-----------
base memory
-----------
/ \
--------- ---------
derived 1 derived 2
--------- ---------

Where derived 1/2 simply read parts of the memory (through a base
method) and return true/false for some access condition checks.

What I wanted to do is instantiate some "derived 1 memory" and cast it
for some other AC checks to "derived 2"

As far as I can see from your answers I should create a new class
which derives from "derived 1" and "derived 2" instead of my casting
approach.

thanks for your replies!

I would create one class that represents the memory, one class that
allows you to access to the memory and derive the two classes from
that one.
This would allow you to access to the same memory object from two or
more different "access" objects.

Paolo Brandoli
 
P

Pete Becker

The fact you had to resort to a C style cast is a strong indicator that
this is a bad idea.

A static_cast would have worked, too.
The only safe way to cast from a Basic* is to use dynamic_cast and for
that to work, the Basic* must point to a Derived.

For a rather restrictive meaning of "safe". If you know that the type
object is Derived (not the case here), and Basic* is not pointing to a
virtual base, static_cast works just fine, and is significantly faster.
 
I

Ian Collins

Pete said:
A static_cast would have worked, too.


For a rather restrictive meaning of "safe". If you know that the type
object is Derived (not the case here), and Basic* is not pointing to a
virtual base, static_cast works just fine, and is significantly faster.
I was referring to the general case, when the conditions above are not
true, a dynamic_cast will fail where a static_cast will give UB.
 

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

Forum statistics

Threads
473,995
Messages
2,570,231
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top