 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gabriel Leblanc Guest
|
Posted: Sun Mar 07, 2004 7:16 pm Post subject: Having a pointer to a parent class ? |
|
|
Is it possible to have a pointer that works like this ?
class ship
| Quote: |
-----------------
|
|
class cruiser class destroyer |
: public ship : public ship
/////////
ship* selectedShip;
if(buttonPressed = 1)
selectedShip = &cruiser1 //Say, have a pointer to an instance of a cruiser
else
selectedShip = &destroyer1 //Say, have a pointer to an instance of a destroyer
Thanks !
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Mar 07, 2004 7:45 pm Post subject: Re: Having a pointer to a parent class ? |
|
|
"Gabriel Leblanc" <gabriel.leblanc (AT) a2m (DOT) com> wrote
| Quote: | Is it possible to have a pointer that works like this ?
class ship
|
|
-----------------
| |
| |
class cruiser class destroyer
: public ship : public ship
/////////
ship* selectedShip;
if(buttonPressed = 1)
selectedShip = &cruiser1 //Say, have a pointer to an instance of a
cruiser
else
selectedShip = &destroyer1 //Say, have a pointer to an instance of a
destroyer
Thanks !
|
Sure. What have you tried?
Example:
#include <iostream>
class Ship
{
public:
virtual void what_am_I() const = 0;
};
class Cruiser : public Ship
{
public:
void what_am_I() const
{
std::cout << "I am a Cruisern";
}
};
class Destroyer : public Ship
{
public:
void what_am_I() const
{
std::cout << "I am a Destroyern";
}
};
void foo(const Ship *p)
{
p->what_am_I();
}
int main()
{
Cruiser c;
Destroyer d;
foo(&c); /* prints "I am a Cruiser" */
foo(&d); /* prints "I am a Destroyer" */
return 0;
}
Which C++ book(s) are you reading?
-Mike
|
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Sun Mar 07, 2004 8:10 pm Post subject: Re: Having a pointer to a parent class ? |
|
|
[email]gabriel.leblanc (AT) a2m (DOT) com[/email] (Gabriel Leblanc) wrote:
| Quote: | Is it possible to have a pointer that works like this ?
class ship
|
|
-----------------
| |
| |
class cruiser class destroyer
: public ship : public ship
/////////
ship* selectedShip;
if(buttonPressed = 1)
selectedShip = &cruiser1 //Say, have a pointer to an instance of a
cruiser
else
selectedShip = &destroyer1 //Say, have a pointer to an instance of a
destroyer
|
How do you *know* that the selectedShip is a cruiser or destroyer?
Assuming you have at least one virtual function in ship, something like
this would do what you want...
ship* selectedShip;
if ( buttonPressed == 1 ) // note the double '='
{
cruiser* cruiser1 = dynamic_cast<cruiser*>(selectedShip);
assert( cruiser1 );
// do stuff with cruiser1
}
else
{
destroyer* destroyer1 = dynamic_cast<destroyer*>(selectedShip);
assert( destroyer1 );
// do stuff with destroyer1
}
But I wouldn't recommend such code.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|