C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Having a pointer to a parent class ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gabriel Leblanc
Guest





PostPosted: Sun Mar 07, 2004 7:16 pm    Post subject: Having a pointer to a parent class ? Reply with quote



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





PostPosted: Sun Mar 07, 2004 7:45 pm    Post subject: Re: Having a pointer to a parent class ? Reply with quote




"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





PostPosted: Sun Mar 07, 2004 8:10 pm    Post subject: Re: Having a pointer to a parent class ? Reply with quote



[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.