1

As I am still somewhat new to programming in C++ I was just curious if it were possible to pass objects pointers to an array in order for code consolidation.

Header file like such;

class.h

class parent
{
    some information.....
};

class child1 : public parent
{
    some information.....
};

class child2 : public parent
{
    some information.....
};

Main file like such;

main.cpp

#include "class.h"

int main()
{
    child1 instanceChild1;
    child2 instanceChild2;

    child1* pointer1 = &instanceChild1;
    child2* pointer2 = &instanceChild2;

    parent array[2] = {pointer1 , pointer2};
}

I am trying to achieve such so that I may create a function that uses a dynamic array in order to hold object pointers so that I may dereference them in the function and manipulate them accordingly. Though I am having issues getting the different pointers to work together when going into an array. I need such functionality since there will be many different objects(all under the same parent) going in and out of this function.

  • Yes it is probably possible. But define `parent* array[2]={pointer1,pointer2};` – Basile Starynkevitch Dec 06 '13 at 06:23
  • Not knowing exactly what you are doing, I would suggest `std::vector` instead of creating a dynamic array. You could add items to the vector this way `vecParents.push_back(&instanceChild1)` – pstrjds Dec 06 '13 at 06:27
  • Another thing to beware of, if you are passing derived objects as base class objects is the [slicing problem](http://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c#274636) – pstrjds Dec 06 '13 at 06:31
  • 1
    @pstrjds Would the slicing problem be non-existent if A was abstract? –  Dec 06 '13 at 06:35
  • @remyabel you are correct if Base class is abstract then there would no slicing issue. – Nik Dec 06 '13 at 06:41
  • The Solution provided by Basile has worked(very simple and I commend you). But I am testing it so far, and yes I am running into the problem of having the object and all its variables(due to them being of class parent), but not having the functions that are specific to the child class that are needed in the function. – user3065238 Dec 06 '13 at 06:43
  • So as of right now I am testing to see if I can get a type test working in order to catch what type of child class it is when dereferencing so that it can be reconstructed in the function properly. If anyone has an easy solution to this I am all ears. – user3065238 Dec 06 '13 at 06:46
  • @user3065238 right, you still have pointers to the base class, so you can only access base class methods. The idea is that you define an "interface" of virtual methods in the base class. Then the derived types can implement these differently. – juanchopanza Dec 06 '13 at 06:47
  • @juanchopanza Ok, yes all is working in harmony now. Thanks for the great input everyone. – user3065238 Dec 06 '13 at 06:54

1 Answers1

0

Yes it is possible. But you need to declare the array like this

parent * arr[] = { ... }

or it would be better if you use a vector

vector<parent *> arr;
arr.push_back(childPointer);//For inserting elements

as @pstrjds and @basile has written and if you want to use child specific member functions, you can use dynamic cast

ChildType1* ptr = dynamic_cast<ChildType1*>(arr.pop());
if(ptr != 0) {
   // Casting was succesfull !!! now you can use child specific methods
   ptr->doSomething();
}
else //try casting to another child class

** your compiler should support RTTI in order for this to work correctly

you can see this answer for details

I prefer to use pure Virtual functions like this

class A {   
        public :
        enum TYPES{ one , two ,three };
        virtual int getType() = 0;
    };
class B : public A{
public:
    int getType()
    {
        return two;
    }
};
class C : public A
{
    public:
    int getType()
    {
       return three;
    }
};
Community
  • 1
  • 1
mchouhan_google
  • 1,775
  • 1
  • 20
  • 28