MWright
MWright

Reputation: 1711

C++ Inheritance. Changing Object data Types

I am having trouble with forcing data type changes has on my own objects. I have a base class say A and two classes derived from A called B and C. I pass objects B and C to a function that checks which type of object it is (B or C). Here is some example code below and the question to my problem:

enum ClassType {"B", "C"};

class A {
  protected:
     m_Type;

  public:
     ClassType Type() { return m_Type}
     ...
     ...
};

class B : public A {
   otherMemberFunctions();

}

 class C : public A {
   otherMemberFunctions();

}


void WhatType(vector<A*>* candidates){


  vector<B*> b_candidates(0);
  vector<C*> c_candidates(0);

  for(int i = 0; i < candidates->size(); i++){

    if(candidates->at(i)->Type() == B ){

      B* b = (B*) candidates->at(i);
      b_candidates(b);
   }

  //Same idea for Object C
  }
}

I would then use WhatType(vector<A*>* candidates) as follows

vector<B*>* b_example

WhatType((vector<A*>*) b_exmaple)

When I have filled the new vector b_candidates in the function WhatType. Will I still have access to the member functions in the B object or will I only have the access to the member functions in the base class A?

I am confused to what happens with the object when I change the type of the object.

Here

WhatType((vector<A*>*) b_exmaple)

and here

B* b = (B*) candidates->at(i);

Upvotes: 3

Views: 2355

Answers (5)

Pepe
Pepe

Reputation: 405

Ok, so if you had an object of type B instantiated on the heap and held by a pointer of type A. you can only see type A's member functions, to access type B's member functions you have to static_cast<B*> which is what the ... "(B*)" ... is doing.

dynamic cast is better as it will return a null if the conversion is not possible. but of course it happens a run-time so there's a penalty.

Upvotes: 1

Matteo Italia
Matteo Italia

Reputation: 126787

When you receive a pointer to a polymorphic object you have two types: the "static" type of the object, which, in your case, will be A *, and its "dynamic" or "real" type, that depends on what was actually assigned to it.

Casting your A * to B * forces the compiler to consider that pointer as a pointer to B; this is safe as long as you actually know that that pointer is actually a pointer to B, otherwise the compiler will start writing nonsensical code (invoking B methods on data of another type).

The checks you are trying to implement are a homegrown version of RTTI, which is a mechanism that allows you to know which is the "real type" of a pointer or a reference to a polymorphic class, and to perform that kind of casts safely. Check out typeid and dynamic_cast on your C++ manual for more info about it. (Incidentally, IIRC dynamic_cast is not only for safety in case the dynamic type is wrong, but it may perform also some extra magic on your pointer if you use it in complicated class hierarchies; so, avoid C-style casting for polymorphic classes)

By the way, in general it's considered "code smell" to have to manually check the "real type" of the pointer in order to cast it and use its methods: the OOP ideal would be being able to do the work only though virtual methods available in the base class.


Big warning: RTTI works only on polymorphic classes, i.e. classes that have at least one virtual method. On the other hand, if you are building a class hierarchy where objects are being passed around as pointers to the base class you'll almost surely want to have a virtual destructor, so that's no big deal.

Upvotes: 1

Tio Pepe
Tio Pepe

Reputation: 3089

As B and C are À derived, a vector<B *> and vector<C *> contains A base class objects. If you ensure to set your A::m_Type attribute in your constructor, you will no have problems:

enum ClassType {'B', 'C'};    // see I modified your definition

class A {
    protected:
         ClassType m_Type;

    public:
         ClassType Type() { return m_Type};
    ...
    ...
};

class B : public A {
    public:
         B() : m_Type('B') {}
         ....
};

Using this, you will check without problems your B and Cobjects. After that, as you are casting base objects to derived ones, you will have fully access to their public methods and attributes.

Upvotes: -1

Some programmer dude
Some programmer dude

Reputation: 409176

The actual type of the objects does not change, of course, but if you only have a pointer (or reference) to the base class you can not access fields specific to the sub-classes.

What you can do to access sub-class fields is to use dynamic_cast to cast it to the sub-class:

A *a = new B;  // We cant reach the members of class B in a
B *b = dynamic_cast<B *>(a);  // But now we have a proper pointer to B

Upvotes: 1

Igor
Igor

Reputation: 27250

Since you cast to B*, you will have access to B's members.

Upvotes: 1

Related Questions