OwnedArray issues


#1

I'm having a rough time wrapping my head around the proper way to pass an array of objects to a child object. The following (and variations I've tried) don't compile.

I have a Daddy object that contains a Child object and an array of Thing objects. My Child object needs to access the array of Things. What is the proper to give my Child object access to the array? (forgive my ignorence...I'm relatively new to C++)

 

class Thing
{
    void doSomething(){};
};


class Child
{
    void init(OwnedArray<Thing>* t)
    {
        theThings = t;

        for(int i = 0; i < theThings->size(); i++)
        {
            theThings[i]->doSomething();
        }
    }
};


class Daddy
{

    void mainInit()
    {
        for(int i = 0; i < 20; i++)
        {
            Thing *t = new Thing();
            myThings.add( t);
        }

        myChild.init(myThings);
    }

    Child myChild;
    OwnedArray<Thing> myThings;
};

Huge thanks.        


#2

The errors with your code above are:

  • All your members are private and can't be accessed. You need to add "public: " after the opening curly bracket of each class definition. Class member functions & variables are private by default.
  • You'd be better off passing a reference than a pointer in init: void init (OwnedArray<Thing>& t). Then just do t[i]->doSomething() in your loop.
  • You don't need theThings local variable. Currently theThings doesn't make sense anyway, because it doesn't have a type. If you keep t as a pointer parameter, you could do this: (*t)[i]->doSomething()

Hope that helps.


#3

My apologies, I was just typing fast. Of course, I know about public. I just left it out for brevity. My code also didn't explain that I need to save the pointer/reference to be used by other functions in Child.

Your last point solved my problem. I was referencing the elements of the array incorrectly. What I needed was this:

(*theThings)[i]->doSomething();

Below is a cleaned up version of my code with the proper referencing to help anyone else.

Thanks again.

class Thing 
{ 
public:
    void doSomething(){}; 
}; 


class Child 
{ 
public:

    void init(OwnedArray<Thing>& t) 
    { 
        theThings = &t;  
    } 

    void otherFunction()
    {
        for(int i = 0; i < (*theThings).size(); i++) 
        { 
            (*theThings)[i].doSomething(); 
        }
    }
    
    OwnedArray<Thing>* theThings;
}; 


class Daddy 
{ 
public:

    void mainInit() 
    { 
        for(int i = 0; i < 20; i++) 
        { 
            Thing *t = new Thing(); 
            myThings.add( t); 
        } 
        
        myChild.init(myThings); 
    } 

    Child myChild; 
    OwnedArray<Thing> myThings; 
};
​

 

 

 

 


#4

Yep that'd work. But perhaps a cleaner way to do it might be to have protected methods in your base:

int getNumThings()

Thing& getThing (int index)

Then your child doesn't need a pointer to the array, which many people would regard as a bit messy and a failure of encapsulation.