Basic Qn: How to identify sub-class object

I have one base Class A and 4 sub Classes B, C, D, E that inherit Class A.

1
2
3
4
5
6
7
8
9
std::vector<A* > myVec;
A* ptr = new B();  // new objects of subclasses created in RANDOM ORDER.
myVec.pushback( ptr );
ptr = new C();
myVec.pushback( ptr );
ptr = new D();
myVec.pushback( ptr );
ptr = new E();
myVec.pushback( ptr );


Assuming we do not know the order in which the objects were inserted in the vector, how can we determine the class of the object say myVec[0].
If the types of your objects are polymorphic, you can print their type names:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <algorithm>
#include <typeinfo>

struct A { virtual ~A() {} };
struct B : A {};
struct C : A {};
struct D : A {};
struct E : A {};

int main()
{
    std::vector<A*> myVec{new B, new C, new D, new E};
    std::random_shuffle(myVec.begin(), myVec.end());
    for(std::size_t n = 0; n < myVec.size(); ++n)
    {
        std::cout << "myVec[" << n << "] points to "
                  << typeid(*myVec[n]).name() << '\n';
        delete myVec[n];
    }
}

$ ./tests  | c++filt -t
myVec[0] points to B
myVec[1] points to E
myVec[2] points to C
myVec[3] points to D


But why would you want that kind of information?
Ok thanks cubbi.

In my application, the user adds different objects to the vector at his will. Each object has very different properties and methods. I want to perform some operations on each element depending on the type of that object.
Each object has very different properties and methods.

Doesn't sound like they are suitable candidates for inheritance, then. If you really need a single vector holding them, and their order is decided at runtime, then a vector of variant types might be more sensible. Or you could describe the requirements in more detail (it's hardly something like "i want you to write a program with std::vector, in which I will put a house, a multiplication, a string, and a type trait")
Sorry for being so vague -).

The proposed application is a simplified electrical control drawing cum simulation software.
Class A is control element class that itself inherits from QGraphicsItem (I'm using Qt)
Class B, C, D etc are different control elements like push button, relay coil, relay NO contact, NC contact etc. Now each of these elements has a distinct set of properties and some inter relations like the coil, NO, and NC are parts of a relay. These are totally different from push buttons.

The idea is that the user be able to construct / modify the control drawing in an easy drag and drop manner. Also if possible, give him an opportunity to simulate it.
Should I divide them further into similar groups of classes?
I would look into loosening the coupling - when I think of a relay coil, it don't see it as something that has a bounding rectangle and a shape, knows how to collide with things or is able to paint itself. It's a physical object with properties useful for simulation, not a graphics item with properties useful for rendering a scene.
Someone who is familiar with Qt may have better ideas, but from a cursory glance, looks like QT lets you store a custom QVariant in a QGraphicsItem, why don't you use those?
I guess you are right. Having a bounding rectangle for my elements was looking wierd to me as well. Will research on qvariant as you suggested.

Thanks for your help!
And sorry I don't understand, what does 'loosening the coupling' mean?

EDIT: ok, I got what you meant. THANKS! Following link explains the phrase:
http://stackoverflow.com/questions/226977/what-is-loose-coupling-please-provide-examples
Last edited on
Topic archived. No new replies allowed.