Best practice when to accessing a derived objects member functions

Concentric

Expert Member
Joined
Feb 16, 2017
Messages
1,028
Reaction score
197
Can someone help me with the best practice when it comes to accessing member functions of a derived class?
As below, i want to access the member function of the derived class, but unless i do something like <static_cast> which i dont think is good practice, i cant figure out what the best way to do it is.

The only thing i could think of is to have all possible functions of derived classes as virutal funcitons in the base class.


C++:
class Shape
{
public:
    Shape();
    virtual void draw() = 0;
}

class Circle: public Shape
{
public:
    Circle();
    void draw();
    void setProperty1(int size);
   
private:
    int m_property1;
}

Shape* shape = new Circle;

shape->setProperty1(12);  //Not possible

Thanks
 
Last edited:
What's the point of instantiating a Shape object if you know you are using a Circle? Rather just create a Circle object then.
 
What is the diameter of a triangle? It has none.
a triangle is a shape.
therefore a shape cannot have a diameter
 
Should you add setDiameter to the base Shape class? Well, do all shapes have the concept of a diameter?
 
What is the diameter of a triangle? It has none.
a triangle is a shape.
therefore a shape cannot have a diameter
Was just using it as an example.
I have edited it.
Some shapes require 1 property to be drawn, and others 2
 
What's the point of instantiating a Shape object if you know you are using a Circle? Rather just create a Circle object then.
There are other derived classes not only Circle, was trying to keep the code brief to focus on my question
 
There are other derived classes not only Circle, was trying to keep the code brief to focus on my question

No I get that. What I mean is at the point where you are calling setDiameter you are obviously aware that you are working with a Circle. So why not use Circle directly?

Code:
Circle* circle = new Circle();
circle.setDiameter(5);
 
No I get that. What I mean is at the point where you are calling setDiameter you are obviously aware that you are working with a Circle. So why not use Circle directly?

Code:
Circle* circle = new Circle();
circle.setDiameter(5);
The derived class type will not be known untill runtime as it depends on user input. I am trying to implement a factory method and factory pattern to create the correct shape when needed.
 
As @Hamster pointed out at the time of calling setProperty you will have to know what type of shape you are dealing with, otherwise you will not be able to call correct property function.
You have two options:
1) do cast at the point of calling setProperty in order to call it for correct object type.
2) create additional type like ShapePropery that will be specialised for each shape in order to contain data that is needed for that particular type.
Declare in base class virtual void setProperty(const ShapeProperty&) and specialise it for each class.

So then when you need to call setProperty you instantiate property object and pass it to shape.

Code:
CircleProperty oCircle(12);

pShape->setProperty(oCircle);

Virtual functions in base class that are based on(related to) specialises classes are NO NO!!
 
Last edited:
It sounds like what you want is a component pattern. So your shape object will have a component or several that make it a circle.
 
The derived class type will not be known untill runtime as it depends on user input. I am trying to implement a factory method and factory pattern to create the correct shape when needed.

If you need to call derivative class specific methods on a parent class, you would need to do some reflection (add a typeID, dynamic_cast, etc.). However, the above usually indicates that you have an issue with your design.

Type specific initialization (eg, setProperty1 on your Circle would ideally be called by the factory (which knows the type it is creating).

Needing to call setProperty1 (specific to Circle), where the code doesn’t know it’s a circle doesn’t make sense. If you think you need this, try find a real case example.
 
Should you add setDiameter to the base Shape class? Well, do all shapes have the concept of a diameter?

Yes, and yes. The distance between the pair of points in the shape with the maximum distance. :) Besides the point though, so setProperty1 is probably more illustrative. :)
 
Top
Sign up to the MyBroadband newsletter
X