Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

GOOSEDUMP

different abstract return types...........

This topic is 6136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was kinda wondering if it was possible to create a situation where two child classes of an absract base class returns different types from the same method call..... ex int child1::foo() { } char* child2::foo() could this be implimented in a way such that the abstract base class does not know the return type of foo()? base* var = new child1(); var->foo(); //returns some int base* var2 = new child2(); var->foo(); //returns some char* One solution would be that foo() is not in the abstract base class so that child1 and child2 could impliment them separately. To access them, you would have to cast... ((child1*)var)->foo(); ((child2*)var2)->foo(); I wonder if their is any elegant way to do this. The situation I was thinking of was encapsulating different storage types into a common interface so calling a Get() would return its dynamic type''s variable... For example, if you have an engine that accepts 2 different internal data types (to do whatever) but loads from the same file on disk (assume you can specify what type of file to convert to, for engine use).. how can this problem be solved? Templates? How?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Yah, templates would be solid.

Share this post


Link to post
Share on other sites
The normal way to handle what I think you are trying to do is to have a static method in the base class, that returns a pointer to the base class and takes an istream as a paramiter. Each class has a constructor, that can construct an object from an istream. The static method reads a "type ID" byte from the stream and dispatches to the correct constructor. Callers can then dynamic_cast to the actuall class type to access members not present in the base, or to avoid the double indirection.

  
class player;
class model;

class ser {
public:
static ser* read(istream &i);
};
ser* ser::read(istream &i) {
char type;
i.read(&type,1);
switch(type) {
case ''p'' : return new player(i);
case ''m'' : return new model(i);
defalt: return 0;
}
}


The problem with this is that any time you want to return more than one type, almost always you either want to interact with it in the same manner regardless of it''s type (polymorphism, just like above) or you want to test to see what type you actually got. If you have the test then you have just been jerking everyone around by having two places with switch statements, and you should have just asked for what you wanted in the first place.

You should also check out the any class at boost for template cleverness.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!