Thread Tools
Old April 26, 2003, 21:51   #1
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
C++ and polymorphism
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.
__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 00:00   #2
Inverse Icarus
Emperor
 
Inverse Icarus's Avatar
 
Local Time: 19:15
Local Date: November 1, 2010
Join Date: May 2001
Location: flying too low to the ground
Posts: 4,625
Re: C++ and polymorphism
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.
__________________
"I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
- Ender, from Ender's Game by Orson Scott Card
Inverse Icarus is offline  
Old April 27, 2003, 00:08   #3
Kuciwalker
Deity
 
Kuciwalker's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Feb 2001
Posts: 21,822
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).
__________________
[Obama] is either a troll or has no ****ing clue how government works - GePap
Later amendments to the Constitution don't supersede earlier amendments - GePap
Kuciwalker is offline  
Old April 27, 2003, 00:11   #4
Inverse Icarus
Emperor
 
Inverse Icarus's Avatar
 
Local Time: 19:15
Local Date: November 1, 2010
Join Date: May 2001
Location: flying too low to the ground
Posts: 4,625
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)
__________________
"I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
- Ender, from Ender's Game by Orson Scott Card
Inverse Icarus is offline  
Old April 27, 2003, 00:17   #5
Kuciwalker
Deity
 
Kuciwalker's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Feb 2001
Posts: 21,822
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.
__________________
[Obama] is either a troll or has no ****ing clue how government works - GePap
Later amendments to the Constitution don't supersede earlier amendments - GePap
Kuciwalker is offline  
Old April 27, 2003, 00:21   #6
Inverse Icarus
Emperor
 
Inverse Icarus's Avatar
 
Local Time: 19:15
Local Date: November 1, 2010
Join Date: May 2001
Location: flying too low to the ground
Posts: 4,625
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.
__________________
"I've lived too long with pain. I won't know who I am without it. We have to leave this place, I am almost happy here."
- Ender, from Ender's Game by Orson Scott Card
Inverse Icarus is offline  
Old April 27, 2003, 00:33   #7
Kuciwalker
Deity
 
Kuciwalker's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Feb 2001
Posts: 21,822
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.
__________________
[Obama] is either a troll or has no ****ing clue how government works - GePap
Later amendments to the Constitution don't supersede earlier amendments - GePap
Kuciwalker is offline  
Old April 27, 2003, 12:34   #8
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
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.
__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 18:49   #9
St Leo
Scenario League / Civ2-CreationApolytoners Hall of Fame
 
St Leo's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Jul 2005
Location: In search of pants
Posts: 5,085
Even storing the class name could get bulky if he has a lot of instances.

Aren't there static variables in C++?
__________________
Blog | Civ2 Scenario League | leo.petr at gmail.com
St Leo is offline  
Old April 27, 2003, 18:56   #10
Harovan
staff
PtWDG Gathering StormPtWDG2 Monty PythonC4DG Gathering Storm
Civ4: Colonization Content Editor
 
Local Time: 01:15
Local Date: November 2, 2010
Join Date: Dec 2001
Posts: 11,117
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.
Harovan is offline  
Old April 27, 2003, 19:01   #11
Kuciwalker
Deity
 
Kuciwalker's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Feb 2001
Posts: 21,822
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?
__________________
[Obama] is either a troll or has no ****ing clue how government works - GePap
Later amendments to the Constitution don't supersede earlier amendments - GePap
Kuciwalker is offline  
Old April 27, 2003, 19:10   #12
Harovan
staff
PtWDG Gathering StormPtWDG2 Monty PythonC4DG Gathering Storm
Civ4: Colonization Content Editor
 
Local Time: 01:15
Local Date: November 2, 2010
Join Date: Dec 2001
Posts: 11,117
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.
Harovan is offline  
Old April 27, 2003, 19:13   #13
Kuciwalker
Deity
 
Kuciwalker's Avatar
 
Local Time: 20:15
Local Date: November 1, 2010
Join Date: Feb 2001
Posts: 21,822
Thanks.

That's pretty useful.
__________________
[Obama] is either a troll or has no ****ing clue how government works - GePap
Later amendments to the Constitution don't supersede earlier amendments - GePap
Kuciwalker is offline  
Old April 27, 2003, 19:26   #14
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
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...
__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 19:38   #15
raghar
Chieftain
 
Local Time: 00:15
Local Date: November 2, 2010
Join Date: Oct 2002
Location: I wish somewhere else.
Posts: 34
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.
raghar is offline  
Old April 27, 2003, 19:58   #16
MattyBoy
Prince
 
MattyBoy's Avatar
 
Local Time: 10:15
Local Date: November 2, 2010
Join Date: May 2001
Location: Pekka Fan Club
Posts: 634
Thanks, Sir Ralph.
That was an excellent explanation.
__________________
"I'm so happy I could go and drive a car crash!"
"What do you mean do I rape strippers too? Is that an insult?"
- Pekka
MattyBoy is offline  
Old April 27, 2003, 20:49   #17
Ted Striker
Apolytoners Hall of Fame
Emperor
 
Ted Striker's Avatar
 
Local Time: 16:15
Local Date: November 1, 2010
Join Date: Dec 1969
Location: Batallón de San Patricio, United States of America
Posts: 3,696
if (pointers_suck) {

try_for_5_minutes($minimum_effort);

} else {

learn_a_new_language($quickly);

}
__________________
"Let the People know the facts and the country will be saved." Abraham Lincoln

Mis Novias
Ted Striker is offline  
Old April 27, 2003, 21:01   #18
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
Ummm, Ted? Wouldn't you try to learn a new language if you think the pointers suck, and not otherwise?
__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 21:04   #19
reds4ever
Prince
 
reds4ever's Avatar
 
Local Time: 01:15
Local Date: November 2, 2010
Join Date: Mar 2001
Location: of the Spion Kop
Posts: 861
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!!
reds4ever is offline  
Old April 27, 2003, 21:04   #20
Ted Striker
Apolytoners Hall of Fame
Emperor
 
Ted Striker's Avatar
 
Local Time: 16:15
Local Date: November 1, 2010
Join Date: Dec 1969
Location: Batallón de San Patricio, United States of America
Posts: 3,696
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!
__________________
"Let the People know the facts and the country will be saved." Abraham Lincoln

Mis Novias
Ted Striker is offline  
Old April 27, 2003, 21:10   #21
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
Well, in that case it should be:
if(tedThinksPointersSuck) {
letHimTryToLearnThemInFiveMinutes();
giveUp(failure::miserable);
} else {
enjoyPointers();
}


__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 21:12   #22
Ted Striker
Apolytoners Hall of Fame
Emperor
 
Ted Striker's Avatar
 
Local Time: 16:15
Local Date: November 1, 2010
Join Date: Dec 1969
Location: Batallón de San Patricio, United States of America
Posts: 3,696


You even put it in courier.

But why are you defending pointers?
__________________
"Let the People know the facts and the country will be saved." Abraham Lincoln

Mis Novias
Ted Striker is offline  
Old April 27, 2003, 21:14   #23
vovan
Apolyton UniversityCivilization IV CreatorsSporeApolyton Storywriters' GuildC3CDG Blood Oath HordeC4DG The Horde
Emperor
 
vovan's Avatar
 
Local Time: 18:15
Local Date: November 1, 2010
Join Date: Oct 2001
Posts: 5,725
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?
__________________
XBox Live: VovanSim
xbox.com (login required)
Halo 3 Service Record (I fail at FPS...)
Spore page
vovan is offline  
Old April 27, 2003, 21:16   #24
Ted Striker
Apolytoners Hall of Fame
Emperor
 
Ted Striker's Avatar
 
Local Time: 16:15
Local Date: November 1, 2010
Join Date: Dec 1969
Location: Batallón de San Patricio, United States of America
Posts: 3,696
That is your idea of fun?

What next, whips and chains you pervert!?!?
__________________
"Let the People know the facts and the country will be saved." Abraham Lincoln

Mis Novias
Ted Striker is offline  
Old April 27, 2003, 21:50   #25
Sirotnikov
DiplomacyApolytoners Hall of FameCivilization III Democracy Game
Emperor
 
Sirotnikov's Avatar
 
Local Time: 02:15
Local Date: November 2, 2010
Join Date: Feb 2000
Posts: 7,138
hey, what's all this trash talks about whips and chains?
Sirotnikov is offline  
Old April 27, 2003, 22:32   #26
Ted Striker
Apolytoners Hall of Fame
Emperor
 
Ted Striker's Avatar
 
Local Time: 16:15
Local Date: November 1, 2010
Join Date: Dec 1969
Location: Batallón de San Patricio, United States of America
Posts: 3,696
Did you get excited Siro?
__________________
"Let the People know the facts and the country will be saved." Abraham Lincoln

Mis Novias
Ted Striker is offline  
Old April 27, 2003, 22:44   #27
Sirotnikov
DiplomacyApolytoners Hall of FameCivilization III Democracy Game
Emperor
 
Sirotnikov's Avatar
 
Local Time: 02:15
Local Date: November 2, 2010
Join Date: Feb 2000
Posts: 7,138
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
Sirotnikov is offline  
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 20:15.


Design by Vjacheslav Trushkin, color scheme by ColorizeIt!.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Apolyton Civilization Site | Copyright © The Apolyton Team