What derived class

 
I need to have a base class and several classes derived from that base class. Since the derived classes are derived from the base class, I can assign the variable (pointer) of any derived class to the variable (pointer) of the base class. So far, everything is clear.
Is there any way to find out what derived class is the pointer that is now stored in the base class variable?

See the example:

class Foo
  {
public:
   void     PrintClass()   {Print("Class Foo");                                                 }
  };

class Bar1 : public Foo
  {
private:
   double   m_value;
public:
   Bar1() : m_value(11.1)  {}
   void     PrintClass()   {printf("Class Bar1 with %s (%.1f)",typename(GetDouble()),m_value);  }
   double   GetDouble()    {return(m_value);                                                    }
  };

class Bar2 : public Foo
  {
private:
   int      m_value;
public:
   Bar2() : m_value(2)     {}
   void     PrintClass()   {printf("Class Bar2 with %s (%d)",typename(GetInt()),m_value);       }
   int      GetInt()       {return(m_value);                                                    }
  };

void OnStart()
  {
   // create variables (pointers)
   Bar1 *bar1=new Bar1;
   Bar2 *bar2=new Bar2;
   Foo  *foo1;
   Foo  *foo2;
   // assign the variables of derived class to the variables of base class
   foo1=bar1;
   foo2=bar2;
   // both variables foo1 and foo2 are now class Foo
   // but there are still pointers to derived classes
   // at this point, can I somehow find out which derived class the pointer is to???? (Bar1 or Bar2)
   foo1.PrintClass();
   foo2.PrintClass();
   // it's OK if I assign the variables of the base class back to the variables of the derived classes
   // I can assign foo2 to bar1 or foo1 to bar2
   bar1=foo1;
   bar2=foo2;
   // I can now use the derived classes
   bar1.PrintClass();
   bar2.PrintClass();
   
   // I can't assign foo2 to bar1 or foo1 to bar2 (runtime error)
   //bar1=foo2;
   //bar2=foo1;
   
   // delete pointers
   if(CheckPointer(bar1)==POINTER_DYNAMIC) delete bar1;
   if(CheckPointer(bar2)==POINTER_DYNAMIC) delete bar2;
  }

I guess I could add a "type" variable to the base class and work with that, but that seems like a very ugly and very last resort.

Something like this:

class Foo
  {
protected:
   int      m_type;
public:
   void     PrintClass()   {Print("Class Foo");                                                 }
   int      Type()         {return(m_type);                                                     }
  };

Is there a better solution?

 
Petr Nosek:
I need to have a base class and several classes derived from that base class. Since the derived classes are derived from the base class, I can assign the variable (pointer) of any derived class to the variable (pointer) of the base class. So far, everything is clear.
Is there any way to find out what derived class is the pointer that is now stored in the base class variable?

See the example:

I guess I could add a "type" variable to the base class and work with that, but that seems like a very ugly and very last resort.

Something like this:

Is there a better solution?

class Foo
  {
protected:
   int      m_type;
public:
   virtual void   PrintClass()=0;                                                  }
   int      Type()         {return(m_type);                                                     }
  };

class Derived1 :public Foo{

void PrintClass(){print("Derived")}

}

class Derived2 : public FOO
{
  




void PrintClass(){print("Derived2")}
}
Use pure virtual function  in base class
 

JamesWoods #
:

Use pure virtual function  in base class

Thank you for your efforts, but using virtual functions is not the solution to my problem. I may have confused you with my example where I used functions to print the class type. This was just an example. In reality, I have several different functions and members in each derived class that are not in the base class. I can't use these functions without proper typing. Therefore, I need to know the derived class.

I'll try an example that will hopefully clear up my problem:

class Foo
  {
public:
   
  };

class Bar1 : public Foo
  {
private:
   double   m_value;
public:
   Bar1() : m_value(11.1)  {}
   double   GetDouble()    {return(m_value);                                                    }
  };

class Bar2 : public Foo
  {
private:
   int      m_value;
public:
   Bar2() : m_value(2)     {}
   int      GetInt()       {return(m_value);                                                    }
  };

void OnStart()
  {
   // derived classes
   Bar1 *bar1=new Bar1;
   Bar2 *bar2=new Bar2;
   // if I want to use an array of elements of derived classes I need to have an array of base class
   Foo *fooArr[2];
   // assing derived class variables to base class array
   fooArr[0]=bar1;
   fooArr[1]=bar2;
   
   // these two lines are unsupported (compiling error)
   // therefore I need to know what derived class is used and stored in base class variable
   //printf("Class Bar1 double value: %.1f",fooArr[0].GetDouble());
   //printf("Class Bar2 int value: %d",fooArr[1].GetInt());
   
   // assign to new correct type variables
   // but I must know what derived class was used for using correct type
   Bar1 *correctType1=fooArr[0];
   Bar2 *correctType2=fooArr[1];
   // I can now use the derived classes
   printf("Class Bar1 double value: %.1f",correctType1.GetDouble());
   printf("Class Bar2 int value: %d",correctType2.GetInt());
   // delete pointers
   if(CheckPointer(bar1)==POINTER_DYNAMIC) delete bar1;
   if(CheckPointer(bar2)==POINTER_DYNAMIC) delete bar2;
  }
 
class C_FooBase
{
    public:

    C_FooBase(string in_typename):
    m_typeName(in_typename)
    {

    }

    string m_typeName;
};

template<class T_Derived>
class C_FOO : public C_FooBase
{
    C_FOO():
    C_FooBase(typename(T_Derived)),
    m_derivedPointer( (T_Derived*)(GetPointer(this)) )
    {
        
    }

    T_Derived* GetTypePtr()
    {
        return m_derivedPointer;
    }

    T_Derived* m_derivedPointer;
    
};

class C_Bar1 : public C_FOO<C_Bar1>
{
    void Bar1Func(){}
};

class C_Bar2 : public C_FOO<C_Bar2>
{
    void Bar2Func(){}
};



void SomeFunc()
{
    C_Bar1 ObjBar1;
    C_Bar2 ObjBar2;

    C_FooBase* pFooArray[2];
    pFooArray[0] = (C_FooBase*)ObjBar1;
    pFooArray[1] = (C_FooBase*)ObjBar2;

    if(pFooArray[0].m_typeName == typename(C_Bar1))
    {
        C_Bar1 * pBar1 = (C_Bar1 *)pFooArray[0]; 
        pBar1.Bar1Func();
    }

    else if (pFooArray[0].m_typeName == typename(C_Bar2))
    {
        C_Bar2 * pBar2 = (C_Bar2 *)pFooArray[0]; 
        pBar2.Bar2Func();
    }


}
not tried to build it , but hope its closer to your fix
 
JamesWoods #:
not tried to build it , but hope its closer to your fix

Thank you again. But this is basically the solution I was talking about in the first post I would like to avoid (if possible). Such a solution could also look like this:

class C_FooBase
  {
public:
   C_FooBase(string in_typename) : m_typeName(in_typename) {}
   string m_typeName;
  };

class C_Bar1 : public C_FooBase
  {
public:
   C_Bar1() : C_FooBase(typename(this)) {}
   void Bar1Func() {Print(m_typeName);}
  };

class C_Bar2 : public C_FooBase
  {
public:
   C_Bar2() : C_FooBase(typename(this)) {}
   void Bar2Func() {Print(m_typeName);}
  };


void SomeFunction(C_FooBase *object)
  {
   if(object.m_typeName==typename(C_Bar1))
     {
      C_Bar1 *pBar1=object; 
      pBar1.Bar1Func();
     }
   else if(object.m_typeName==typename(C_Bar2))
     {
      C_Bar2 *pBar2=object; 
      pBar2.Bar2Func();
     }
  }

void OnStart()
  {
   C_Bar1 *ObjBar1=new C_Bar1();
   C_Bar2 *ObjBar2=new C_Bar2();
   C_FooBase *pFooArray[2];
   pFooArray[0]=ObjBar1;
   pFooArray[1]=ObjBar2;
   SomeFunction(pFooArray[0]);
   SomeFunction(pFooArray[1]);
   if(CheckPointer(ObjBar1)==POINTER_DYNAMIC) delete ObjBar1;
   if(CheckPointer(ObjBar2)==POINTER_DYNAMIC) delete ObjBar2;
  }

Imagine you are working with a ready-made library with one base class and many derived classes. You don't want to edit the code of this library. You need to create an array of base class elements. You can assign an object of any derived class to each element of this array. But then the problem is finding out what type of derived class is used in that element. Personally, I don't think you can do that without editing the ready-made library (like the example above). But just to be sure, I'm trying a forum query to see if I've missed something.

So the question is: Is it possible (without editing the ready-made library) to find out what derived class is in the base class object? (I don't think so)

 
What about
if(dynamic_cast<C_Bar1 *>(pFooArray[i])!=NULL) 
  {
    C_Bar1 *obj=pFooArray[i];
    double val=obj.GetDouble();
  }
*-But to add, adding the GetDouble and GetInt methods in derived classes breaks the OOP (is a relation) in regards to those methods, because they are not part of base, so now you are looking for a technical solution for design problem, which is not to say it's definetely wrong.
 
Petr Nosek #:

Thank you again. But this is basically the solution I was talking about in the first post I would like to avoid (if possible). Such a solution could also look like this:

Imagine you are working with a ready-made library with one base class and many derived classes. You don't want to edit the code of this library. You need to create an array of base class elements. You can assign an object of any derived class to each element of this array. But then the problem is finding out what type of derived class is used in that element. Personally, I don't think you can do that without editing the ready-made library (like the example above). But just to be sure, I'm trying a forum query to see if I've missed something.

So the question is: Is it possible (without editing the ready-made library) to find out what derived class is in the base class object? (I don't think so)

Why don't you just rename both methods GetInt and GetDouble to GetValue and then cast it as needed? 

 
Amir Yacoby #:
What about
*-But to add, adding the GetDouble and GetInt methods in derived classes breaks the OOP (is a relation) in regards to those methods, because they are not part of base, so now you are looking for a technical solution for design problem, which is not to say it's definetely wrong.

Thank you Amir. Dynamic casting is a solution! I've missed it. Then the code could look like:

class C_FooBase
  {
  };

class C_Bar1 : public C_FooBase
  {
public:
   void Bar1Func() {Print(typename(this));}
  };

class C_Bar2 : public C_FooBase
  {
public:
   void Bar2Func() {Print(typename(this));}
  };

void SomeFunction(C_FooBase *object)
  {
   if(dynamic_cast<C_Bar1 *>(object)!=NULL)
     {
      C_Bar1 *pBar1=object; 
      pBar1.Bar1Func();
     }
   else if(dynamic_cast<C_Bar2 *>(object)!=NULL)
     {
      C_Bar2 *pBar2=object; 
      pBar2.Bar2Func();
     }
  }

void OnStart()
  {
   C_Bar1 *ObjBar1=new C_Bar1();
   C_Bar2 *ObjBar2=new C_Bar2();
   C_FooBase *pFooArray[2];
   pFooArray[0]=ObjBar1;
   pFooArray[1]=ObjBar2;
   SomeFunction(pFooArray[0]);
   SomeFunction(pFooArray[1]);
   if(CheckPointer(ObjBar1)==POINTER_DYNAMIC) delete ObjBar1;
   if(CheckPointer(ObjBar2)==POINTER_DYNAMIC) delete ObjBar2;
  }

P.S..

This problem could be related to the standard graphical library from Metaquotes. The base class is CWnd and derived classes are CEdit, CButton, CLabel... Each of these derived classes has its own methods and members.

Reason: