Cast to derived class?

Discussion in 'C++' started by christian.pontesegger@googlemail.com, Nov 12, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
     
    Ian Collins, Nov 12, 2007
    #2
    1. Advertising

  3. Guest

    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, 2007
    #3
  4. Guest

    On Nov 12, 10:42 am, 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, 2007
    #4
  5. Pete Becker Guest

    On 2007-11-12 03:01:55 -0500, Ian Collins <> said:

    > 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)
     
    Pete Becker, Nov 12, 2007
    #5
  6. Ian Collins Guest

    Pete Becker wrote:
    > On 2007-11-12 03:01:55 -0500, Ian Collins <> said:
    >
    >> 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.
     
    Ian Collins, Nov 12, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    736
    Ivan Vecerina
    Oct 12, 2003
  2. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
  4. Replies:
    5
    Views:
    390
    Alf P. Steinbach
    Nov 13, 2007
  5. David
    Replies:
    3
    Views:
    438
    Grizlyk
    Jan 29, 2008
Loading...

Share This Page