-1

I have an Ellipsoid struct, which is a derivative of the Object struct. Basically, I want to make an Object[3][13] pointer array in a Scene struct, in which I can point to different Object derivatives.

struct Ellipsoid : public Object {
      //...
}

In the main function:

Ellipsoid ellipsoids[13];
addEllipsoids(3, ellipsoids); // this fills the array with valid ellipsoids, everything seems to be fine up until this point

// printing the contents here, everything is good

scene.addObjects(ellipsoids);

After printing the "ellipsoids" array here, everything seems nice.

in the Scene struct:

struct Scene{
   int arr_num;
   Object* objects[3];

   void addObjects(Object* o){
        // printning the o[0], o[1], ... contents here, getting garbage..
        objects[arr_num++] =  o;
}

When I print out the *o contents here (from 0..12) even before I add them to the objects array, I get nasty memory garbage results.

This is for homework and I can't use std::vector and such things, only very basic stuff. I really have no idea what is the problem here.

Matt
  • 14,906
  • 27
  • 99
  • 149
Hadron
  • 33
  • 5
  • possible duplicate of [Can a pointer to base point to an array of derived objects?](http://stackoverflow.com/questions/7196172/can-a-pointer-to-base-point-to-an-array-of-derived-objects) – eerorika Nov 05 '14 at 13:14
  • If there's one thing that you learn from the course, I hope it is that plain arrays are horrible and you'll appreciate standard containers when you're allowed to finally use them. – eerorika Nov 05 '14 at 13:23
  • Ah, yes, thanks, I didn't find that question, now I get it. – Hadron Nov 05 '14 at 14:14

2 Answers2

1

The issue is you are passing derive class array to a base class pointer. This is not inheritance. Refer:

http://www.parashift.com/c++-faq/array-derived-vs-base.html

Need to look at complete code but this could solve the problem:

Object ellipsoids[13];

rockoder
  • 747
  • 11
  • 23
  • Thanks, now I finally understand why it didn't work. This wasn't the solution for me though (for several reasons), I had to create seperate functions to add Ellipsoids, Paraboloids etc. to the array. Thanks anyway for the help. – Hadron Nov 05 '14 at 14:13
0

Instead of void addObjects(Object* o) ; use void addObjects(Object o [] , int noOfArrayElements ) noOfArrayElements is required because you need the number of array elements that you can process in addObject .

your scene structure can store only 3 Object . So you should make sure that from outside you get only 3 objects and not more than that . in your comments you passed 13 object ? Or if you want to make it generic try using malloc .

your scene object does not makes arr_num initialised to 0 . hope you are doing it somewhere ?

MAG
  • 2,841
  • 6
  • 27
  • 47
  • 1. sorry, perhaps it was badly worded, but what I meant is that I need an array of Object **pointers** . The "objects" array is the one that contains these pointers. The passed "ellipsoid" array happens to be size of 13, but it could be bigger. 2. yes, arr_num is initalized in the constructor of Scene. – Hadron Nov 05 '14 at 13:00