Apolyton Archive  |  Preserved copy of the Apolyton Civilization Site and its forums as they stood in September 2005. Read-only; nothing here can be posted to or replied to.  |  Forum index |  About this archive |  The 1998–2001 UBB forums
Today on Apolyton WARDELL INTERVIEW PROMO A.C.S. HISTORY CHAPTER 4 GET CIV4 /w FREE PLUS! A.C.S. PHOTO GALLERY GET A.O.M. V1.1
Apolyton Civilization Forums
main| civ2| civ3| civ4| smac| ctp2| ron| moo3| galciv| galciv2| alt| about|
ApolytonPLUS | register | search | faq | new posts | pm (-/-) | upload | members
hall of fame new! | civgroups | civgroups news | interviews | the column | radio | chat | directory | news | store | PLUS
Apolyton Civilization Forums : Powered by vBulletin version 2.0.3 Apolyton Civilization Forums > Miscellaneous > Archive > Off-Topic-Archive > C++ and polymorphism
Show a Printable Version | Email This Page to Someone! | Receive updates to this thread | Report this to Apolyton news!

bottom of page
  
Author
Thread    < Last Thread     Next Thread > Post New Thread     Post A Reply
vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 27-04-2003 06:51
Edit/Delete Message Reply w/Quote
#1 Report this post to a moderator
C++ and polymorphism Support Apolyton, buy Galactic Civilizations

Okay, first off: I have been spoiled by Java.



So, now, I am going back to programming in C++ and I am having a slight problem. In Java I made much use of the instanceof operator to determine which derived class an object was an instance of. Now, sure enough, I was coding in C++ the other day and wanted to use that same thing, but... It looks like there is no such thing. So the question is:

Suppose I have some base class, and a bunch of derived classes. I also have a method that should process an object. And that object can be an instance of any of the derived classes. So, I specify the type of the parameter as the base class. However, for bookkeeping purposes, I would still need to know exactly which of the derived classes the parameter is an instance of. Is there any way at all to do it? (Short of putting an identifyier string member into the base class and initializing it to different values in the derived classes?)

Any help would be appreciated.

Inverse Icarus is offline Inverse Icarus
Emperor
flying too low to the ground
May 2001
time: 00:27
  Old Post 27-04-2003 09:00 Visit Inverse Icarus's homepage!
Edit/Delete Message Reply w/Quote
#2 Report this post to a moderator
Re: C++ and polymorphism Support Apolyton, pre-order Civilization IV

quote:
Originally posted by vovansim
(Short of putting an identifyier string member into the base class and initializing it to different values in the derived classes?)


shot my idea down.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:27
  Old Post 27-04-2003 09:08
Edit/Delete Message Reply w/Quote
#3 Report this post to a moderator
Help yourself to an AD-FREE life

No way. I'm pretty sure the instanceof operator only works in Java because of reflection, which I'm pretty sure can only work in an interpreted language (otherwise things such as field and method names are lost).

Inverse Icarus is offline Inverse Icarus
Emperor
flying too low to the ground
May 2001
time: 00:27
  Old Post 27-04-2003 09:11 Visit Inverse Icarus's homepage!
Edit/Delete Message Reply w/Quote
#4 Report this post to a moderator
Help yourself to an AD-FREE life

i'd just use templated functions with a name member var, if you really need to know what class it is (although, ideally, you should never need to know)

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:27
  Old Post 27-04-2003 09:17
Edit/Delete Message Reply w/Quote
#5 Report this post to a moderator
Support Apolyton buy from Amazon

Uber - that's not true. Say you're retrieving something dynamically from a hashtable. It contains a bunch of "Airplanes", but some of these are "CivilianAirplanes" and others are "MilitaryAirplanes", which have different methods and fields. To know what to cast it as, you have to know which subclass it's an instance of. Storing the data in a string would work, but could be VERY memory-intensive.

Inverse Icarus is offline Inverse Icarus
Emperor
flying too low to the ground
May 2001
time: 00:27
  Old Post 27-04-2003 09:21 Visit Inverse Icarus's homepage!
Edit/Delete Message Reply w/Quote
#6 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

quote:
Originally posted by skywalker
Uber - that's not true. Say you're retrieving something dynamically from a hashtable. It contains a bunch of "Airplanes", but some of these are "CivilianAirplanes" and others are "MilitaryAirplanes", which have different methods and fields. To know what to cast it as, you have to know which subclass it's an instance of. Storing the data in a string would work, but could be VERY memory-intensive.


i didn't mean store all the data in the string, i meant store the class name if you needed to.

i'm saying that if he's writing a function that works on all plane objects, he shouldn't need to know what kind of plane it is. and if it's plane specific, it should be a member function of the derivative class anyway.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:27
  Old Post 27-04-2003 09:33
Edit/Delete Message Reply w/Quote
#7 Report this post to a moderator
Support Apolyton buy from Amazon

Even storing the class name could get bulky if he has a lot of instances.

If it's plane specific, he has to know what type of plane to know which methods he can call.

vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 27-04-2003 21:34
Edit/Delete Message Reply w/Quote
#8 Report this post to a moderator
Support Apolyton, pre-order Civilization IV

Alright. Thanks. That's what I thought. Just making sure I am not missing something here. I think I have found a little workaround for my issue. Thanks for the feedback any ways.

St Leo is offline St Leo
King of Scenario League
Member of the Apolyton Social Democratic Party
Jul 2005
time: 00:27
  Old Post 28-04-2003 03:49 Visit St Leo<br><img src=/forums/images/staff-icon.gif>'s homepage!
Edit/Delete Message Reply w/Quote
#9 Report this post to a moderator
Support Apolyton, buy Civilization: The Boardgame

Even storing the class name could get bulky if he has a lot of instances.

Aren't there static variables in C++?

Sir Ralph is offline Sir Ralph
Emperor
Long live teh Schwampel!
Dec 2001
time: 06:27
  Old Post 28-04-2003 03:56
Edit/Delete Message Reply w/Quote
#10 Report this post to a moderator
Support Apolyton, pre-order Civilization IV

Nobody would put a identifier string in every instance. Better to define a pure method in the base class, like

virtual const char* ClassName() = 0;

and overwrite it in the derived classes, like this:

class MyDerivedClass : public MyBaseClass {
...
virtual const char* ClassName();
...
};

const char* MyDerivedClass::ClassName()
{
return "MyDerivedClass";
}

In this case, the ID string exists only once per application.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:27
  Old Post 28-04-2003 04:01
Edit/Delete Message Reply w/Quote
#11 Report this post to a moderator
Help yourself to an AD-FREE life

I have very little experience with c++. What does virtual do?

However, since the program itself doesn't know what class the object is an instance of, wouldn't it call the wrong method?

Sir Ralph is offline Sir Ralph
Emperor
Long live teh Schwampel!
Dec 2001
time: 06:27
  Old Post 28-04-2003 04:10
Edit/Delete Message Reply w/Quote
#12 Report this post to a moderator
Suffering from ads?

virtual makes you call the latest derived version of a function. For instance, you have the mentioned class Airplane and derived classes CivilianAirplane and MilitaryAirplane. Now you have a pointer

Airplane* myPtr;

and assign a pointer to an object of one of the derived classes to it, let's say a pointer to a CivilianAirplane. Note that the content of the pointer is seen as instance of the base class Airplane. Now you call the method ClassName:

std::cout << myPtr->ClassName() << std::endl;

and in the standard output appears the string "CivilianAirplane", although myPtr points to the class Airplane and not CivilianAirplane. If the function was not virtual, the function Airplane::ClassName would be called.

Note that pure methods (those that end with = 0) are always virtual.

Kuciwalker is offline Kuciwalker
Emperor
of Schmooism
Feb 2001
time: 00:27
  Old Post 28-04-2003 04:13
Edit/Delete Message Reply w/Quote
#13 Report this post to a moderator
Support Apolyton, buy Call to Power 2

Thanks.

That's pretty useful.

vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 28-04-2003 04:26
Edit/Delete Message Reply w/Quote
#14 Report this post to a moderator
Support Apolyton

quote:
Originally posted by Sir Ralph
Nobody would put a identifier string in every instance. Better to define a pure method in the base class.


Right, that's what I meant. Sorry if I didn't make it clearer...

raghar is offline raghar
Chieftain
I wish somewhere else.
Oct 2002
time: 05:27
  Old Post 28-04-2003 04:38
Edit/Delete Message Reply w/Quote
#15 Report this post to a moderator
Support Apolyton, buy Call to Power 2

So because a lot of C programmers is here. A little question.

So imagine DLL file. I know name of method in it and I would like to call it from Java. I know I need to have wrapper by JNI however how can I do wrapper. (There might be a little problem if I find no compiller. )

Could you write a compilable example? Use NewLibrary.DLL as name of library.

MattyBoy is offline MattyBoy
Prince
Pekka Fan Club
Jun 2001
time: 15:27
  Old Post 28-04-2003 04:58
Edit/Delete Message Reply w/Quote
#16 Report this post to a moderator
Lose 30 kilos (of popups)

Thanks, Sir Ralph.
That was an excellent explanation.

Ted Striker is offline Ted Striker
Emperor
United States of America
Jan 1970
time: 21:27
  Old Post 28-04-2003 05:49 Visit Ted Striker's homepage!
Edit/Delete Message Reply w/Quote
#17 Report this post to a moderator
Help yourself to an AD-FREE life

if (pointers_suck) {

try_for_5_minutes($minimum_effort);

} else {

learn_a_new_language($quickly);

}

vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 28-04-2003 06:01
Edit/Delete Message Reply w/Quote
#18 Report this post to a moderator
Support Apolyton, buy Alpha Centauri

Ummm, Ted? Wouldn't you try to learn a new language if you think the pointers suck, and not otherwise?

reds4ever is offline reds4ever
Prince
of the Spion Kop
Mar 2001
time: 05:27
  Old Post 28-04-2003 06:04
Edit/Delete Message Reply w/Quote
#19 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

quote:
Originally posted by vovansim
Ummm, Ted? Wouldn't you try to learn a new language if you think the pointers suck, and not otherwise?


and pointers DO suck!!

Ted Striker is offline Ted Striker
Emperor
United States of America
Jan 1970
time: 21:27
  Old Post 28-04-2003 06:04 Visit Ted Striker's homepage!
Edit/Delete Message Reply w/Quote
#20 Report this post to a moderator
Support Apolyton, buy Galactic Civilizations: Deluxe Edition

quote:

Ummm, Ted? Wouldn't you try to learn a new language if you think the pointers suck, and not otherwise?


Well you try for 5 minutes and then you learn another language.

I don't need a human debugger damn you!

vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 28-04-2003 06:10
Edit/Delete Message Reply w/Quote
#21 Report this post to a moderator
Suffering from ads?

Well, in that case it should be:

if(tedThinksPointersSuck) {
letHimTryToLearnThemInFiveMinutes();
giveUp(failure::miserable);
} else {
enjoyPointers();
}


Ted Striker is offline Ted Striker
Emperor
United States of America
Jan 1970
time: 21:27
  Old Post 28-04-2003 06:12 Visit Ted Striker's homepage!
Edit/Delete Message Reply w/Quote
#22 Report this post to a moderator
Support Apolyton, buy Galactic Civilizations



You even put it in courier.

But why are you defending pointers?

vovan is offline vovan
Emperor

Oct 2001
time: 06:27
  Old Post 28-04-2003 06:14
Edit/Delete Message Reply w/Quote
#23 Report this post to a moderator
Support Apolyton, buy GURPS/ Alpha Centauri

quote:
Originally posted by Ted Striker
But why are you defending pointers?


They are fun. The fact that Java manages them automatically doesn't make them any less fun, now does it?

Ted Striker is offline Ted Striker
Emperor
United States of America
Jan 1970
time: 21:27
  Old Post 28-04-2003 06:16 Visit Ted Striker's homepage!
Edit/Delete Message Reply w/Quote
#24 Report this post to a moderator
Support Apolyton, buy Civilization 2

That is your idea of fun?

What next, whips and chains you pervert!?!?

Sirotnikov is offline Sirotnikov
King
<--- Yes, It's a "kitty" in a boot.
Feb 2000
time: 07:27
  Old Post 28-04-2003 06:50
Edit/Delete Message Reply w/Quote
#25 Report this post to a moderator
Inflate your Upload Space

hey, what's all this trash talks about whips and chains?

Ted Striker is offline Ted Striker
Emperor
United States of America
Jan 1970
time: 21:27
  Old Post 28-04-2003 07:32 Visit Ted Striker's homepage!
Edit/Delete Message Reply w/Quote
#26 Report this post to a moderator
Support Apolyton, buy Galactic Civilizations

Did you get excited Siro?

Sirotnikov is offline Sirotnikov
King
<--- Yes, It's a "kitty" in a boot.
Feb 2000
time: 07:27
  Old Post 28-04-2003 07:44
Edit/Delete Message Reply w/Quote
#27 Report this post to a moderator
Support Apolyton, buy Civilization III: Complete

I already am.

Don't you realize I'm constantly using the search function to seek out any instance of bondage on poly, and comment about it

Remember the thread about the "marry a millionaire" contestant who did some bondage flicks? I may possible have encountered her work in some way or another

  < Last Thread     Next Thread > Post New Thread     Post A Reply
All times are GMT. The time now is 05:27.
Apolyton Time is 00:27.
    top of page
Rate This Thread:
archivepost
Forum Jump:
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is ON
vB code is ON
Smilies are ON
[IMG] code is ON
 




Contact Us - Apolyton Civilization Site - Support Us!

Building a better Apolyton through better information. Click here and take our poll!
Non-US visitors, click here!

Powered by: vBulletin Version 2.0.3
Copyright ©2000, 2001, Jelsoft Enterprises Limited.

Page generated in 0.0551 seconds (93.20% PHP - 6.80% MySQL) with 31 queries
Page Loading Time:

Support Apolyton: Amazon USA | Amazon UK | Amazon DE | Amazon FR |
Support Apolyton and get FREE PLUS, Buy from Chips&Bits: Galactic Civilizations | Galactic Civilizations: Deluxe Edition | Call to Power 2 | Civilization: The Boardgame | GURPS/ Alpha Centauri | Alpha Centauri | Civilization IV | Civilization III: Complete |


Front Page | Civilization IV | Civilization III | Civilization II | Call to Power II | Alpha Centauri | Master of Orion III
Rise of Nations | Galactic Civilizations | Galactic Civilizations II | Misc
Alt.Civs | Civ I | C:CtP I | About | News | Directory | Apolyton Store | Forums | Chat | Columns | Interviews | Newsletter
Scenario League | CSC | Clash of Civs | Spanish Site | CtP Maps | Cradle of Civ | WesW's Ctp1/2 Site | Civ3 Haven

apolyton.net | apolyton.com | civilization2.net | civilization3.net | civilization4.net | civilizationiv.info | calltopower.net | galciv.net | galciv2.net | moo3.net