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

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.

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

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.

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

>>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.

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;

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>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.