Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ polymorphism of a object in an array

Tags:

People also ask

What is polymorphism in array?

Polymorphism: The ability for the same code to be used with different types of objects and behave differently with each.

What is polymorphism C++?

Polymorphism in C++ means, the same entity (function or object) behaves differently in different scenarios. Consider this example: The “ +” operator in c++ can perform two specific functions at two different scenarios i.e when the “+” operator is used in numbers, it performs addition.

How do I make a pointer array in C++?

An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like: int *ptr[5]; // array of 5 integer pointer.


I'm an embedded software engineer and coming from the world of bits and C. In that world, there are data in flash memory represented by const in C. And there are data in RAM. RAMs are expensive and limited, while flash memory is cheap and enough. Also, dynamic memory allocation using new, delete, malloc etc is not allowed due to fragmentation problem or safety regulations, static designs are preferred.

I've around 2000 objects which have similar constant properties but different behaviors. So for them, I defined Shape Class as a base class which holds shared properties of my objects. And to represent different behavior, Shape Class has one abstract method called Print() which will be overwritten by parents.

ShapeList is the important part. It is a const array consists of "const Shapes" so that they will be placed into flash memory section by linker.

Below program produces an output:

I'm a Shape has 3 dots
I'm a Shape has 4 dots
I'm a Shape has 5 dots

While expected output is:

I'm a Triangle has 3 dots
I'm a Rectangle has 4 dots
I'm a Pentagon has 5 dots

I need polymorphic behavior. When I print Triangle, it should behave like a Triangle, not as a Shape. How can I do this?

Thanks.

#include <array>
#include <cstdio>
class Shape
{
    public:
    const int DotCount;
    Shape(const int dot): DotCount(dot) {}
    virtual void Print(void) const; // this is virtual method
};

void Shape::Print(void) const
{
    printf("I'm a Shape has %d dots\n", DotCount);
}

class Triangle: public Shape
{
    public:
    Triangle(void): Shape(3) { }
    void Print(void) const;
};

void Triangle::Print(void) const
{
    printf("I'm a Triangle has %d dots\n", DotCount);
}

class Rectangle: public Shape
{
    public:
    Rectangle(void): Shape(4) { }
    void Print(void) const;
};

void Rectangle::Print(void) const
{
    printf("I'm a Rectangle has %d dots\n", DotCount);
}

class Pentagon: public Shape
{
    public:
    Pentagon(void): Shape(5) { }
    void Print(void) const;
};

void Pentagon::Print(void) const
{
    printf("I'm a Pentagon has %d dots\n", DotCount);
}

const std::array<const Shape, 3> ShapeList = { Triangle(), Rectangle(), Pentagon() };

int main(void)
{
    ShapeList.at(0).Print();
    ShapeList.at(1).Print();
    ShapeList.at(2).Print();
    return(0);
}

More problem: Today I realized that there is another problem with virtual functions. When I add any virtual functions into base class, compiler start ignoring "const" directive and place object automatically to the RAM instead of flash memory. I don't know why. I've asked this question to IAR. The conclusion I got so far is that Polymorphic behavior is not possible with ROMable classes even with heap :/