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

Cast to derived class?

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>

Nov 12 '07 #1
5 2460
ch*******************@googlemail.com wrote:
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:
<snip/>
>
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.

--
Ian Collins.
Nov 12 '07 #2
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!
--
Christian Pontesegger

Nov 12 '07 #3
On Nov 12, 10:42 am, christian.ponteseg...@googlemail.com wrote:
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!
--
Christian Pontesegger
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

Nov 12 '07 #4
On 2007-11-12 03:01:55 -0500, Ian Collins <ia******@hotmail.comsaid:
ch*******************@googlemail.com wrote:
>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:
<snip/>
>>
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.
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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 12 '07 #5
Pete Becker wrote:
On 2007-11-12 03:01:55 -0500, Ian Collins <ia******@hotmail.comsaid:
>ch*******************@googlemail.com wrote:
>>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:
<snip/>
>>>
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.

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 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.

--
Ian Collins.
Nov 12 '07 #6

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

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
11
by: Alberto Giménez | last post by:
Hi, I've seen some object oriented programming bits out there and i'm not sure if they're legal. For example: struct Object { int field1; int field2; }; struct SubObject { int field1; /*...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
6
by: Ron M. Newman | last post by:
Hi, Under what circumstances can I ever get an exception about not being able to cast type "ABC" to type "ABC" if there's only one ABC class in the system and it's loaded from a dynamic...
11
by: Dinsdale | last post by:
I am trying to determine what the current cast of an object is. Essentially, if I use GetType, it always returns the original type of the object created but I want to know what the current cast of...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
2
by: christian.pontesegger | last post by:
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.