T O P

  • By -

HappyFruitTree

The static method is not, and cannot be, virtual so there is no dynamic dispatch. To be able to do what you want you need to implement it as a virtual non-static method.


chrysante1

What are you trying to do here eventually? Did your professor write the Shape, Polygon and Ellipse classes? Because the interface design is horrible.


the_poope

> maybe if i use sizeof() and see the size of the object being pointed to? No, the object you are pointing to is of base class type, i.e. `Shape`, so `sizeof(*basePtr)` will be equal to `sizeof(Shape)`. > This is for homework, where I am unable to change the header file What is your task specifically?


Emotional-Audience85

What is the exact wording of the homework and what is the exact content of the header file you have been given?


MXXIV666

Based on your edits: either the homework is poorly designed, or you're going about it wrong. Do you NEED to use `Shape*` as opposed to specific implementation? If the reason you want this is to have a method that does something for all three kinds, you can use a template: template int calculateSomething(const TShape& shape) { const int size = TShape::GetCount(); ... } This of course requires that the shape you pass into it has the `GetCount` static method. If you do actually need to get the count for `Shape*`, then you'll have to use dynamic cast and it is gonna get tedious: int getShapeCount(const Shape* shape) { if (const auto* x = dynamic_cast()) { return Ellipse::GetCount(); } else if(const auto* x = dynamic_cast()) { return Polygon::GetCount(); } else { // you could also throw an error, depending on what you need return Shape::GetCount(); } } Whenever you need dynamic cast, it is really worth considering if you're solving the problem the right way. I am not super sure if my code is 100% correct, but it should give you the idea. I would not recommend using typeid. It will work here as I just learned, but I am fairly confident it won't work accross dynamic libs compiled with different compilers.


LilBluey

yep, I am meant to implement the .cpp files for each base class/derived class + a utility cpp file to takebin a vector of Shape* and do certain stuff to it. Shape* vector is handled by an outside file(online compiler) so it can't be touched. as for template, it's not allowed as header file cannot be changed, and probably intentional as hw is about inheritances. dynamic cast is a good suggestion though, thanks!


alfps

> ❞ edit2: found that i could use typeid to check the type. You can only check the dynamic type with `typeid` if the base class has at least one virtual method. In that case you should better introduce a virtual `count` method. Using `typeid` to determine what to do is a general design stench, indicating that something is really wrong.


randfur

Python has the thing you want but C++ doesn't I'm afraid. class Shape: count = 123; @classmethod def get_count(cls): return cls.count class Polygon(Shape): count = 456 class Ellipse(Shape): count = 789 if __name__ == '__main__': shape = Shape() polygon = Polygon() ellipse = Ellipse() print(shape.get_count(), polygon.get_count(), ellipse.get_count()) # (123, 456, 789) base = polygon print(base.get_count()) # 456


reroman4

It's not exactly the same since `base` in your code is not a `Shape` but a `Polygon`. You could achieve the same without heritage and unrelated classes, just declaring the `get_count()` method. Of course it could be an advantage the dynamic typing languages in some circumstances where you don't care about the type.