Thread: another c++ cast question

  1. #1
    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
    28,413
    That might work, but what is somefunc() supposed to do? There might be a better way of doing it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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
    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, 08:36 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM