another c++ cast question

This is a discussion on another c++ cast question within the C++ Programming forums, part of the General Programming Boards category; Code: class base { public: char m; } class derived : public base { public: char c; } void somefunc(base ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    another c++ cast question

    Code:
    class base {
    public:
    	char m;
    }
    
    class derived : public base {
    public:
    	char c;
    }
    
    void somefunc(base **bc) { }
    
    int main() {
    	derived *ptr = 0;
    	somefunc(reinterpret_cast<base**>(&ptr));
    	return 0;
    }
    Is this a right way to do it in C++?
    Normally I would just do somefunc((base**)&ptr); and it would work just fine..
    I tried dynamic_cast but it didnt work.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    That might work, but what is somefunc() supposed to do? There might be a better way of doing it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Somefunc is just an 'interface' for WinAPI function, so that I dont have specify all arguments each time I call it (I can save lines).

    Shouldn't I do this kind of casts with dynamic_cast?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Can you not write it as:
    Code:
    void somefunc(base* bc) { /* ... */ }
    If so, all you need is to pass the derived pointer, no casting would be needed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The code is very dangerous. It suggests that somefunc will assign a value to the pointer, such as this:
    Code:
    void somefunc(base **bp)
    {
      *bp = new derived;
    }
    This way, after somefunc returns, ptr points to a derived object. So far, so fine. But look, this is also perfectly valid for somefunc to do:
    Code:
    void somefunc(base **bp)
    {
      *bp = new base;
    }
    It's just a pointer to base, so you can assign the address of a base object to it, right? Or any object derived from base, perhaps other_derived.

    On the other hand, if you do that, suddenly ptr points not to a derived, but a base or an other_derived. Welcome to the land of hidden bugs.

    In other words, while casting a pointer to a derived class to a pointer to a base class is perfectly safe and can be done implicitly, casting a pointer to a pointer to a derivd class to a pointer to a pointer to a base class is a big no-no.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Ok, this is how I have changed it:

    Code:
    void somefunc(base *&bc) {
    	//I reinterpret cast it here before I pass it to WinAPI
    }
    
    int main() {
    	derived *ptr = 0;
    	somefunc(ptr);
    	return 0;
    }
    Did you mean this?
    Is there any other way to do this?

    Thanks for help again

    BTW, laserlight is that girl on avatar you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie cast question
    By 200thhorseman in forum C++ Programming
    Replies: 4
    Last Post: 07-05-2009, 02:25 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. question
    By Gil22 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 07:36 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21