954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting Derived Types From A List Of Base Types

Hi,

I have a list of base types which stores multiple derived types. The following code shows a simplified setup.

#include "Base.h"
#include "DerivedA.h"
#include "DerivedB.h"
#include <iostream>
#include <list>

void main()
{
	std::list<Base*> base;
	std::list<Base*> *basePointer = &base;
	for (int i = 0; i < 10; i++)
	{
		basePointer->push_back(new DerivedA);
		basePointer->push_back(new DerivedB);
	}
}


I iterate through this list using the iterator below:

std::list<Base*>::iterator baseItt = basePointer->begin();
baseItt++;


Is there a way I can find out which derived type 'baseItt' is pointing to? I need this to help me with saving and loading in my program.

Please let me know if this is not clear enough, and thanks in advance for any help you can provide.

Stemiros

stemiros
Newbie Poster
5 posts since Dec 2010
Reputation Points: 14
Solved Threads: 0
 

You can add some 'type' variable to the base class, that you set through its constructor.

class base {
  enType type_;
public: base( enType type ) : type_( type ) {} 
  enType getType() const { return type_; }
};

struct derive : public base
 derive() : base( TYPE_DERIVE_1 ) { }
};


When you call functions 'through' the base*, you're calling them on the derived object.
So, in the base class make a getType function that derived must overload.

struct base {
   virtual enType getType() = 0;
};

struct derive : public base
  enType getType() const { return DERIVED_TYPE; }  
};


Or you can template the base class based on enType.

I'm sure there are more ways, but these are the 3 I can think of right now.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Solved Threads: 75
 

Thanks for your help but I don't know (and can't find) how to use enType in my application. Any suggestions? It seems strange that there isn't an inbuilt function enabling you to see the type of an object, once it has been created.

Would adding a simple variable to each derived class, like a character which is set when an object is created, work in the same way?

Stemiros

stemiros
Newbie Poster
5 posts since Dec 2010
Reputation Points: 14
Solved Threads: 0
 

I had a sentence explaining enType in my post but for some reason I removed it.

enType can be for example an enum, or a char.. whatever you want. Adding a variable to each derived class as you suggest works as well, but using e.g. the virtual getType function makes things easier to maintain, as you force every class the inherits from base to implement the getType function - it won't compile otherwise.

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Solved Threads: 75
 

I see, thanks for that. I'll stick to the simple variable for now, but I'll experiment with enType when I have a longer deadline.

Thanks again for the help

Stemiros

stemiros
Newbie Poster
5 posts since Dec 2010
Reputation Points: 14
Solved Threads: 0
 

>>Is there a way I can find out which derived type 'baseItt' is pointing to? I need this to help me with saving and loading in my program.

Consider having a save function for each class instead.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This is a good question.
If your base class has a virtual function, then typeid() will find out what type of derived object is your iterator pointing to.

cout << typeid(*(*baseItt)).name() << endl;
mrinal.s2008
Light Poster
28 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

This is a good question. If your base class has a virtual function, then typeid() will find out what type of derived object is your iterator pointing to.

cout << typeid(*(*baseItt)).name() << endl;

Sorry. Please include the header for typeid as below.

#include<typeinfo>
mrinal.s2008
Light Poster
28 posts since Dec 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: