1

I have this function

# include "Rectangle.hh"
# include "Circle.hh"

void inst_obj (int symbols) {

  for (int i=0; i<symbols ; i++) {
    if (i<10) {
      Rectangle symb(1,2,3);
      //Store symb in an array, like symb_array[i] = symb;
    }
    else {
      Circle symb(1,2,3,4);
      //Store symb in an array, like symb_array[i] = symb;
    }

  }// inst_obj

Then i have another function:

  void check_symbols(symbols) {

    for (int i=0; i<symbols; i++) {
      // Check symbol objects, like symb_array[i].return_something
    }
  } // check_symbols

How can you store the pointers to the different objects in an easy way? (and how do you then access them)?

  • What is the greater design? Do you have a base class like `Shape` from which `Rectangle` and `Circle` derive? Do you need a container to store created objects? Please be specific of what you need and explain what you'd done thus far. Containers are generally binded to a single element type in C++, unless you do some type erasure. – legends2k Oct 11 '13 at 10:33

3 Answers3

2

You could return a vector of pointer which points to base objects out from inst_obj and pass it to check_symbols() function. Like this:

// assume Shape is base type of Rectangle and Circle

 std::vector<std::unique_ptr<Shape>> inst_obj (int symbols)
 {
    std::vector<std::shared_ptr<Shape>> v;

   for (int i=0; i<symbols ; i++) 
   {
     if (i<10) 
     {
      v.push_back(std::unique_ptr<Shape>(new Rectangle (1,2,3)));
     }
     else 
     {
       v.push_back(std::unique_ptr<Shape>(new Circle (1,2,3,4)));
     }
     return v;
  }

void check_symbols(std::vector<std::unique_ptr<Shape>>& v) 
{
    for(auto it = v.begin(), it != v.end(); ++it)
    {
      // Check symbol objects, like symb_array[i].return_something
    }
} 

Usage:

std::vector<std::unique_ptr<Shape>> v = inst_obj(43);
check_symbols(v);
billz
  • 44,644
  • 9
  • 83
  • 100
0

you can create a base class Shape, make both Rectangle and Circle inherit from that class,and make your array of type Shape *.

this array can hold pointers to Rectangle and Circle objects because both are Shape objects.

Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53
  • 1
    Make your array of type `Shape*` (or some other pointer equivalent). Polymorphism requires pointers or references, it does not work on objects. – john Oct 11 '13 at 10:35
  • 1
    @Mhd.Tahawi: Don't mistake me but your edit is still wrong. Not because the OP wants but in C++ for runtime polymorphism one needs pointers/reference, otherwise one would end up with the problem of [slicing](http://stackoverflow.com/q/274626/183120). – legends2k Oct 11 '13 at 10:40
  • @legends2k: your comment is most welcome :) please check if I still have any mistakes in my answer, thanks for sharing your experience. – Moha the almighty camel Oct 11 '13 at 10:46
-1

You can use inheritance for this purpose.

Have a base class Shape(preferably an abstract class with pure virtual methods like draw() etc.). Inherit Rectangle, Circle and other shapes from the Shape.

An array or a vector of Shape can hold instances of any objects that are inherited from the Shape class.

We need to use an array of pointer to Shape to have the polymorphism work properly.

Pai
  • 282
  • 2
  • 3
  • Same mistake as the other answer, Polymorphism requires pointers or references, it does not work on objects. The correct statement is - An array or vector of Shape pointers can hold pointers to derived objects and access those objects through virtual functions. – john Oct 11 '13 at 10:37