alternative to casting to derived type

Discussion in 'C++' started by jimmy, May 18, 2005.

  1. jimmy

    jimmy Guest

    Hi,

    Please forgive me if this is elementary. I can't seem to find the
    solution anywhere.

    // foo() is only declared/defined in Derived
    Base* base = new Derived();
    ((Derived*)base)->foo();

    Given that I would like to use a Base*; is casting the only solution?

    -J
    jimmy, May 18, 2005
    #1
    1. Advertising

  2. jimmy

    Rolf Magnus Guest

    jimmy wrote:

    > Hi,
    >
    > Please forgive me if this is elementary. I can't seem to find the
    > solution anywhere.
    >
    > // foo() is only declared/defined in Derived
    > Base* base = new Derived();
    > ((Derived*)base)->foo();
    >
    > Given that I would like to use a Base*; is casting the only solution?


    Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
    or static_cast.
    Rolf Magnus, May 18, 2005
    #2
    1. Advertising

  3. jimmy

    ben Guest


    > Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
    > or static_cast.


    dynamic_cast may NOT work if Base has no virtual function at all.
    static_cast is a better alternative.

    ben
    ben, May 18, 2005
    #3
  4. ben wrote:
    >
    > > Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
    > > or static_cast.

    >
    > dynamic_cast may NOT work if Base has no virtual function at all.


    But in situations like the one the OP is in, the Base class
    usually has at least one virtual function: the destructor.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 18, 2005
    #4
  5. ben wrote:
    >
    > > Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
    > > or static_cast.

    >
    > dynamic_cast may NOT work if Base has no virtual function at all.


    Besides: Can you quote where this is nailed down in the C++ standard?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 18, 2005
    #5
  6. jimmy

    ben Guest


    > But in situations like the one the OP is in, the Base class
    > usually has at least one virtual function: the destructor.


    How do you know? I've seen plausible designs where no virtual function is
    involved in inheritance.

    ben
    ben, May 18, 2005
    #6
  7. jimmy

    ben Guest


    > Besides: Can you quote where this is nailed down in the C++ standard?
    >


    hmm, I don't have the standard (can't afford it) but if you try to
    dynamic_cast a non polymorphic type the compiler's not gonna do it for you.

    ben
    ben, May 18, 2005
    #7
  8. ben wrote:
    >
    > > But in situations like the one the OP is in, the Base class
    > > usually has at least one virtual function: the destructor.

    >
    > How do you know? I've seen plausible designs where no virtual function is
    > involved in inheritance.


    Because the OP posted:

    > Base* base = new Derived();
    > ((Derived*)base)->foo();


    And since I assume that he is a good guy he will eventuall delete
    the object. And for that he needs a virtual destructor or he
    has undefined behaviour in his program.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 18, 2005
    #8
  9. jimmy

    ben Guest


    > Because the OP posted:
    >
    > > Base* base = new Derived();
    > > ((Derived*)base)->foo();


    without virtual destructors:

    Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
    delete ptr_to_derived;
    ben, May 18, 2005
    #9
  10. jimmy

    ben Guest

    oops, I wonder why I wrote "reinterpret_cast"...and it is an ugly example
    just to show it would work. It is not what I meant "plausible design" though

    ben

    > Ok, besides the fact that it's wrong (*): Do you really think this is a
    > plausible design?
    >
    > (*) You have to use a static_cast, not a reinterpret_cast.
    >
    ben, May 18, 2005
    #10
  11. jimmy

    Rolf Magnus Guest

    ben wrote:

    >
    >> Because the OP posted:
    >>
    >> > Base* base = new Derived();
    >> > ((Derived*)base)->foo();

    >
    > without virtual destructors:
    >
    > Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
    > delete ptr_to_derived;


    Ok, besides the fact that it's wrong (*): Do you really think this is a
    plausible design?

    (*) You have to use a static_cast, not a reinterpret_cast.
    Rolf Magnus, May 18, 2005
    #11
  12. * jimmy:
    >
    > Please forgive me if this is elementary. I can't seem to find the
    > solution anywhere.
    >
    > // foo() is only declared/defined in Derived
    > Base* base = new Derived();
    > ((Derived*)base)->foo();
    >
    > Given that I would like to use a Base*; is casting the only solution?


    Preferably 'foo' should be a virtual function declared in 'Base' and
    overridden in 'Derived'.

    Failing that -- if 'foo' is not meaningful in 'Base', but you still
    need to handle heterogenous collection of 'Base' pointers -- you should
    use the visitor pattern.

    Finally, if that isn't an option either, then you have a fundamental design
    error and should redesign.

    A static_cast can technically do the job instead of one of the above, but it
    means you're doing assembly-language-level programming instead of C++.

    As others have mentioned, the cast you're using is _even worse_ than a
    static_cast: it's akin to using an assembly language where you _don't
    know_ what machine code will be emitted for an instruction, the assembler
    choosing whatever it likes the most at the moment. Old C style casts
    in C++ generally means (1) complete newbie, (2) Microsoft employee, or (3)
    incompetent but non-MS. (There is one thing C style casts can do that C++
    casts can't, but that's advanced.) So if for nothing else, then at least
    for your own image, avoid C-style casts, completely.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, May 18, 2005
    #12
  13. ben wrote:
    >
    > > Because the OP posted:
    > >
    > > > Base* base = new Derived();
    > > > ((Derived*)base)->foo();

    >
    > without virtual destructors:
    >
    > Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
    > delete ptr_to_derived;


    Just a moment.
    Are we fooling around right now or are we talking about
    plausible real world programming?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, May 18, 2005
    #13
  14. jimmy

    Ron Natalie Guest

    Karl Heinz Buchegger wrote:
    > ben wrote:
    >
    >>>Yes. But don't, I repeat, DON'T use C style casts for it. Use dynamic_cast
    >>>or static_cast.

    >>
    >>dynamic_cast may NOT work if Base has no virtual function at all.

    >
    >
    > Besides: Can you quote where this is nailed down in the C++ standard?
    >


    It's not very hard, it's nailed down in 5.2.7 where they describe
    the dynamic_cast operator. Unless you're converting a null pointer,
    converting something to itslef, or doing a derived to base conversion,
    the class pointed or referred to SHALL BE POLYMORPHIC
    Ron Natalie, May 18, 2005
    #14
  15. jimmy

    Ron Natalie Guest

    ben wrote:
    >>Because the OP posted:
    >>
    >>
    >>>Base* base = new Derived();
    >>>((Derived*)base)->foo();

    >
    >
    > without virtual destructors:
    >
    > Derived* ptr_to_derived = reinterpret_cast<Base*>(base);
    > delete ptr_to_derived;
    >
    >

    That won't compile, perhaps you wanted to cast to Derived*?
    Further, a static_cast would be safer.
    Ron Natalie, May 18, 2005
    #15
    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. Frederic Rentsch

    Type casting a base class to a derived one?

    Frederic Rentsch, Jan 11, 2007, in forum: Python
    Replies:
    11
    Views:
    1,329
    Chris Mellon
    Jan 24, 2007
  2. Replies:
    4
    Views:
    869
    Ian Collins
    Mar 30, 2007
  3. Replies:
    1
    Views:
    394
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    386
    Victor Bazarov
    May 23, 2007
  5. Rui Maciel
    Replies:
    9
    Views:
    272
    Rui Maciel
    May 13, 2010
Loading...

Share This Page