|
-
October 4th, 2010, 07:01 PM
#1
Differentiating inherited objects
I'm in over my head on this project I'm working on, and it's been driving me crazy for the past few days. I'll try to explain this as best as I can, without being too vague.
This project relies on multiple data types, each of which is encapsulated in its own class. Although these data objects are all different, they are all of the same base type, which is not a valid type by itself, so it will likely be an abstract base class (although I don't know if I can make it an ABC.)
At some point during the program, I will be instantiating different derived types based on user input, and these will be assigned to a pointer of the base class. This is necessary due to the mechanism I'm using to keep track of these objects, and work with them. I need some way to diferentiate the derived types, since the only way I can reference them is through a pointer of the base class.
I tried something as follows:
Code:
// base.hpp
enum EDataType { UnknownType, TypeA, TypeB };
class Base
{
public:
EDataType GetDataType() { return _dataType; }
protected:
EDataType _dataType;
};
// typea.hpp
#include "base.hpp"
class DerivedA : public Base
{
public:
DerivedA() { _dataType = TypeA; }
// ...more stuff here...
};
// typeb.hpp
#include "base.hpp"
class DerivedB : public Base
{
public:
DerivedB() { _dataType = TypeB; }
// ...more stuff here...
};
Now there are several problems with this approach.
1) "Base" is not actually an abstract base class, so there's nothing preventing the user from instantiating it if they want to.
2) This is not easily extensible. In order to add new types, I would have to manually expand the enumeration in the base file, since C++ does not support enumeration inheritance/extension.
I thought I'd try a different approach: each class will have an abstract GetType() function which will return some unique identifier, possibly a string, as follows:
Code:
// base.hpp
class Base
{
public:
virtual static string GetType() = 0;
};
The idea here being is that now Base is an ABC, and all derived classes will implement GetType() and return a unique identifier. But of course I cannot declare a function to be both virtual and static.
So I'm kind of stuck. Ultimately I need a way to differentiate between classes, as my code is something as follows:
Code:
Base* data;
.......
if (dataOne->GetType() == /* something here */)
{
....
}
else if (dataOne->GetType() == /* etc */)
{
...
}
As I mentioned at the beginning, I'm using pointers to the base class due to the way I'm passing data around. If I need to change around the architecture of my program to make this easier I'd be willing to do so.
Can I just use RTTI? I've read about checking the result of dynamic_cast, but honestly that seems a bit clunky. There must be an easy way of doing this, by checking the return of some identifier function in each class.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|