In this code I've started working on, I came across a common pattern that somehow doesn't sit right with me. Usually, it involves an enum, a map, a switch and some sort of class hierarchy. I've tried to abstract a MWE:
#include <iostream>
#include <map>
class Shape {
public:
virtual double SumOfInternalAngles() { throw std::exception(); }
};
class Triangle : public Shape {
public:
double SumOfInternalAngles() { return 180.0; }
};
class Rectangle : public Shape {
public:
double SumOfInternalAngles() { return 360.0; }
};
enum TeamShapes {AlicesTriangle, BobsRectangle, CarolsTriangle};
int main()
{
Triangle alicesTriangle;
Rectangle bobsRectangle;
Triangle carolsTriangle;
std::map<TeamShapes, Shape*> shapeMap;
shapeMap[TeamShapes::AlicesTriangle] = &alicesTriangle;
shapeMap[TeamShapes::BobsRectangle] = &bobsRectangle;
shapeMap[TeamShapes::CarolsTriangle] = &carolsTriangle;
for(auto it : shapeMap)
{
switch (it.first)
{
case TeamShapes::AlicesTriangle:
std::cout << it.second->SumOfInternalAngles() << std::endl;
break;
case TeamShapes::BobsRectangle:
std::cout << static_cast<Rectangle*>(it.second)->SumOfInternalAngles() << std::endl;
break;
}
}
return 0;
}
There seems to be repetitive information, and both versions of accessing the member function have drawbacks: In the first case, you need to have a virtual member function in the base class, which means that all the derived classes get "cluttered" with functions that don't really make sense for them, e.g. a Circle would end up with a function getCorners(). In the second case I would prefer not needing the cast, although I know that it is necessary. Maybe someone can point me in a direction where I can come up with a better design for this case.
I'm quite new to C++, so I'd like to hear what the "best practices" and "conventions" regarding such constructs are. Maybe the code is fine, and I simply need to adjust?
If you make your base class pure virtual (i.e make the class abstract), then you don't have to implement SumOfInternalAngles() in it and thus no need to throw an exception. Shape then becomes an abstract interface to be implemented by a derived class and this derived class MUST implement SumOfInternalAngles().
Then in your switch statement, you don't need to cast unless you wanted to call a method specific to the derived class such as getCorners() which may or may not be present in all derived versions of Shape.
To do this simply change you shape definition to
class Shape {
public:
virtual double SumOfInternalAngles() = 0;
};
And use the first version of your switch case.
i.e
case TeamShapes::AlicesTriangle:
std::cout << it.second->SumOfInternalAngles() << std::endl;
break;
case TeamShapes::BobsRectangle:
std::cout << it.second->SumOfInternalAngles() << std::endl;
break;
EDIT: some sample code to try and help illustrate.
#include <iostream>
#include <string>
class base
{
public:
virtual std::string AMethodThatMustBeImplemented() = 0;
virtual std::string ABaseMethod() { return std::string("base::ABaseMethod"); }
};
class A : public base
{
public:
virtual std::string AMethodThatMustBeImplemented() { return std::string("A::AMethodThatMustBeImplmented"); }
// No need to implment ABaseMethod here unless we wanted to!
};
class B : public base
{
public:
virtual std::string AMethodThatMustBeImplemented() { return std::string("B::AMethodThatMustBeImplmented"); }
virtual std::string ABaseMethod() { return std::string("B::ABaseMethod"); }
};
int main(int argc, char** argv)
{
//base obj; // can't do this since base has 'pure virtual' called AMethodThatMustBeImplemented.
A objA;
B objB;
std::cout << objA.AMethodThatMustBeImplemented() << '\n';
std::cout << objA.ABaseMethod() << '\n';
std::cout << objB.AMethodThatMustBeImplemented() << '\n';
std::cout << objB.ABaseMethod() << '\n';
base& b = static_cast<base&>(objB);
std::cout << b.ABaseMethod() << " <- notice still calling B::ABaseMethod\n";
std::cout << b.base::ABaseMethod() << " <- ah-ha now calling base::ABaseMethod\n";
}
Maybe what you want is an intermediate interface for your derived class, in this case a "Polygon" interface derived from "Shape", which has methods that make sense for polygons, but not for a circle for example :
#include <iostream>
#include <map>
#include <math.h>
class IShape {
public:
virtual double getArea() = 0;
};
class IPolygon : public IShape {
public:
virtual double sumOfInternalAngles() = 0;
};
class Triangle : public IPolygon {
public:
Triangle(double _base, double _height) : base(_base), height(_height) {}
double sumOfInternalAngles() { return 180.0; }
double getArea() { return (base * height)/2; }
private:
double base;
double height;
};
class Rectangle : public IPolygon {
public:
Rectangle(double _width, double _height) : width(_width), height(_height) {}
double sumOfInternalAngles() { return 360.0; }
double getArea() { return (width * height); }
public:
double width;
double height;
};
class Circle : public IShape {
public:
Circle(double _radius) : radius(_radius) {}
double getArea() { return (3.14 * pow(radius,3)); }
public:
double radius;
};
enum TeamShapes {AlicesTriangle, BobsRectangle, CarolsCircle};
int main()
{
Triangle alicesTriangle(10,10);
Rectangle bobsRectangle(10,10);
Circle carolsCircle(10);
std::map<TeamShapes, IShape*> shapeMap;
shapeMap[TeamShapes::AlicesTriangle] = &alicesTriangle;
shapeMap[TeamShapes::BobsRectangle] = &bobsRectangle;
shapeMap[TeamShapes::CarolsCircle] = &carolsCircle;
for(const auto& it : shapeMap)
{
switch (it.first)
{
case TeamShapes::AlicesTriangle:
case TeamShapes::BobsRectangle:
std::cout << static_cast<IPolygon*>(it.second)->sumOfInternalAngles() << std::endl;
std::cout << it.second->getArea() << std::endl;
break;
case TeamShapes::CarolsCircle:
std::cout << it.second->getArea() << std::endl;
break;
}
}
return 0;
}
You still have to cast your pointer, but I find it preferable to a non virtual method which throw an exception.
Related
So I saw this thread: What does it mean to "program to an interface"?. Which talks about declaring a Parent, but initializing it as a Child. Is it possible to do the same but with c++? For example: I have an interface Shape which can be implemented as Triangle or Square.
I tried to do the following but my program didn't compile:
Shape * myShape = new Square();
myShape->uniquetoSquare();
"typeinfo for Shape", referenced from:
typeinfo for Triangle in Triangle.o
class Shape {
public:
Shape(){};
int height;
int width;
string color;
void sayHello(){
cout << "Hello!";
}
int getArea(){
return 0;
}
}
class Triangle: public Shape {
public:
bool obtuse;
Triangle(){
obtuse = false;
};
void sayHello(){
cout << "Hello I'm a triangle!";
}
int getArea(){
return height*width / 2;
}
}
class Square: public Shape {
public:
bool rectangular
Square(){
rectangle = true;
};
void sayHello(){
cout << "Hello I'm a square!";
}
int getArea(){
return height*width;
}
void uniqueToSquare(){
cout << "This func is only in square!";
}
}
Shape does not have a function named uniqueToSquare. Remember that if you are using a Shape, then you can only use shape-like methods.
If you want to use it as a Square, then do something like:
Square* mySquare = dynamic_cast<Square*>(myShape);
if (mySquare != nullptr) mySquare->uniqueToSquare();
This is a simple polymorphism example. Yes it is possible in C++ and is actually the heart of the benefits of Object-Oriented design
With C++ it isn't as simple as you might hope. You need virtual functions to do what you want. Modifying your example to be "good" C++.
class Shape {
public:
Shape(){}
int height;
int width;
string color;
virtual void sayHello() const {
cout << "Hello!";
}
virtual int getArea() const {
return 0;
}
//virtual destructor, no need to declare this on the derived types
virtual ~Shape() {}
}
class Triangle : public Shape {
public:
bool obtuse;
Triangle() {
obtuse = false;
}
void sayHello() const {
cout << "Hello I'm a triangle!";
}
int getArea() const {
return height*width / 2;
}
}
class Rectangle : public Shape {
public:
bool square;
Rectangle() {
square = false;
}
void sayHello() const {
cout << "Hello I'm a rectangle!";
}
int getArea() const {
return height*width;
}
void uniqueToRectangle() const {
cout << "This func is only in rectangle!";
}
}
The error your getting seems to be because of missing runtime type information RTTI. You may want to enable this but it really is unnecessary for what you want to achieve. Use dynamic cast as others have suggested (which also uses RTTI).
Shape* myShape = new Rectangle();
((Rectangle*)myShape)->uniquetoRectangle();
This is OK but remember to delete myShape; before it goes out of scope. See RAII for the use of destructors for that.
Your variable myShape is a pointer to a Shape. Your class Shape does not have a method called uniqueToSquare(). Therefore, you have an error.
The solution is to initialise myShape as a Square simply by Square myShape = Square();
Remember that it is always better to preserve the real type of an object, this is better for performance, and basically gives more information to the compiler. You only resort to dynamic polymorphism when you really have to (ie, storing polymorphic objects in an array).
How can I access sideA and height members of Triangle class, and how can I access sideA of a Square class, these are both derived from Shape class?
What is the correct way to implement that?
Shapes.h:
class Shape
{
public:
virtual double getArea() = 0;
};
class Triangle : public Shape
{
public:
double sideA = 3;
double height = 2;
double getArea() {
return 0.5 * sideA * height;
}
};
class Square : public Shape
{
public:
double sideA = 4;
double getArea() {
return sideA * sideA;
}
};
Main.cpp:
int main()
{
Shape* sh = new Triangle();
std::cout << sh->getArea() << std::endl;
std::cout << sh->??? //get the height of triangle
delete sh;
}
You are trying to access information that is not available via the interface you defined, class Shape allows only the area to be accessed.
To get also the height, the proper way is to extend the interface to provide that information as well.
class Shape
{
public:
virtual double getArea() = 0;
virtual double getHeight() = 0;
};
class Triangle : public Shape
{
public:
double sideA = 3;
double height = 2;
double getArea() {
return 0.5 * sideA * height;
}
double getHeight() {
return height;
}
};
class Square : public Shape
{
public:
double sideA = 4;
double getArea() {
return sideA * sideA;
}
double getHeight() {
return sideA;
}
};
A Shape has no height. You are using the triangle polymorphically. That means you have a Shape* and can only use the interface of Shape, no matter what is the actual type of the object. If you want a Triangle then use a Triangle not a Shape. If you still want to use a Triangle and Rectangle polymorphically, then you should put the common interface into the base class. In your case, both have a sideA, so you could do:
struct Shape {
double sideA = 3;
virtual double getArea() = 0;
virtual ~Shape(){}
};
struct Triangle : public Shape {
double height = 2;
double getArea() {
return 0.5 * sideA * height;
}
};
struct Square : public Shape {
double getArea() {
return sideA * sideA;
}
};
int main() {
Shape* sh = new Triangle();
std::cout << sh->sideA;
delete sh;
}
PS: the above wasn't the whole truth. If you have a Shape* and you know that it is a Triangle* then you could use dynamic_cast, but doings such casts are often a sign for poor design. You should strive to write classes such that you do not need a cast.
Because your base class has a virtual function1, you can use the dynamic_cast conversion to check if a pointer to it is actually a pointer to one of its derived classes. This will return nullptr if it is not of the 'tested' class, or a usable pointer to the derived class, if it is:
int main()
{
Shape* sh = new Triangle();
std::cout << sh->getArea() << std::endl;
if (dynamic_cast<Square*>(sh) != nullptr) { // Check for a valid Square pointer
Square* sq = dynamic_cast<Square*>(sh);
std::cout << sq->sideA << std::endl;
}
else if (dynamic_cast<Triangle*>(sh) != nullptr) { // Check for a valid Trianlge pointer
Triangle* tr = dynamic_cast<Triangle*>(sh);
std::cout << tr->height << std::endl;
}
else {
std::cout << "Unspecified shape type: height unknown!" << std::endl;
}
delete sh;
return 0;
1 Note that, because you have a virtual function in your Shape class, you should also give it a virtual destructor:
class Shape {
public:
virtual double getArea() = 0;
virtual ~Shape() { }
};
For further discussion on the need for a virtual destructor, see here: When to use virtual destructors?.
EDIT: In your specific case, the answer given by rustyx is really the 'correct' approach; however, it is useful to understand/appreciate the use of the dynamic_cast option, as this can be the only solution if you are deriving classes from a third-party base class, which you cannot modify, and thus cannot add the equivalent of the getHeight() function to it.
You can declare the variable a Triangle* not a Shape*, this way you'll have access to the derived class and base class methods and variables:
int main()
{
Triangle* sh = new Triangle();
Square* sh2 = new Square();
std::cout << sh->getArea() << std::endl; //3
std::cout << sh2->getArea() << std::endl; //16
std::cout << sh->sideA << std::endl; //3
std::cout << sh2->sideA << std::endl; //4
delete sh;
}
To use delete sh safely you should have a virtual destructor
class Shape
{
public:
virtual double getArea() = 0;
virtual ~Shape(){} //virtual destructor
};
Since you already have an abstract class, why not use it to access the the data in the derived classes:
Here is how I would do it:
#include <iostream>
#include <memory>
class Shape
{
private:
double sideA; //shared members can be defined in base class, assuming all
//derived classes will have sideA member
protected:
Shape(double sideA) : sideA(sideA) {}//for initialization of sideA in derived classes
public:
Shape() = default;
virtual double getArea() = 0;
double getSideA() { //shared logic
return sideA;
}
virtual ~Shape(){} //virtual destructor
};
class Triangle : public Shape
{
private:
double height = 2; //specific property
public:
Triangle() : Shape(3) {} //intialize sizeA
double getHeight(){ //specific method, must instanciate Triangle to access
//for collections it's best to use interface method like getArea()
return height;
}
double getArea() override {
return 0.5 * getSideA() * height;
}
};
class Square : public Shape
{
public:
Square() : Shape(4) {} //intialize sizeA
double getArea() override {
return getSideA() * getSideA();
}
};
int main()
{
std::unique_ptr<Shape> sh(new Triangle); //smart pointer
std::unique_ptr<Shape> sh2(new Square); //smart pointer
std::cout << sh->getArea() << std::endl; //3
std::cout << sh2->getArea() << std::endl; //16
std::cout << sh->getSideA() << std::endl; //3
std::cout << sh2->getSideA() << std::endl; //4
//delete sh; //no need, smart pointer
}
Take a look at smart pointers.
The function "foo" is from external library (In my case DEAlII ).
It takes in class type as template parameter and its object. It then creates a copy of this object and performs some operation on this object.
The problem is when the input class is polymorphic (template ). Then I always pass base class object but pointing to different derived class, but the copied object in the function "foo" will be base class.
And any call to member function on this copied object will call base class member function(But I need them to call to corresponding derived class member function).
Additionally the type of derived class is decided at runtime based on some input parameter. I cannot change/ move to different library, but I should be able to modify the function "foo" in the library(preferably not but may be as a last resort).
#include <iostream>
#include <memory>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0){
width = a;
height = b;
}
void set(int a){
width =a ;
}
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
virtual ~Shape()=default;
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area () override{
cout << "Rectangle class area :" <<width*height <<endl;
return (width * height);
}
};
class Triangle: public Shape {
public:
Triangle( int a = 0, int b = 0):Shape(a, b) { }
int area () {
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
Triangle(const Triangle &triangle){
width = triangle.width;
height = triangle.height;
}
};
template <class temp>
void foo (temp &shape){
shape.area();
temp shape_sample = shape;
shape_sample.area();
}
// Main function for the program
int main() {
unique_ptr<Shape> shape;
Rectangle rec(10,7);
shape =make_unique<Rectangle> (rec);
foo (*shape.get());
shape->area();
return 0;
}
If the implementation of foo can't be change then I see two options here:
Option 1: Simply downcast to appropriate type if you know what it is:
foo(static_cast<Rectangle &>(*shape.get()));
Option 2 (overkill): hide the polymorphism using Bridge pattern:
class Shape {
protected:
class ShapeImpl {
public:
int width, height;
virtual ~ShapeImpl() = default;
// "virtual copy constructor" which you could've used without a Bridge
// if you could change "foo"
virtual ShapeImpl *clone() { return new ShapeImpl(*this); }
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
} *impl; // can't use unique_ptr because we want copy
Shape(ShapeImpl *impl)
: impl(impl) { }
public:
Shape(const Shape &other)
: impl(other.impl ? other.impl->clone() : nullptr) {
}
Shape(Shape &&other)
: impl(nullptr) {
std::swap(impl, other.impl);
}
virtual ~Shape() {
delete impl;
}
// copy-and-swap idiom with one assignment operator to "rule-of-five them all"
Shape &operator=(Shape other) {
std::swap(impl, other.impl);
return *this;
}
int area() {
return impl->area();
}
};
class Rectangle : public Shape {
protected:
class RectangleImpl : public ShapeImpl {
public:
ShapeImpl *clone() override { return new RectangleImpl(*this); }
int area() override {
cout << "Rectangle class area :" <<width*height <<endl;
return (width * height);
}
};
public:
Rectangle(int width = 0, int height = 0)
: Shape(new RectangleImpl())
{
impl->width = width;
impl->height = height;
}
};
If the implementation of the foo could not be changed. one other option is to write a wrapper around it and using dynamic cast send the right type to it.
template <class temp>
void foo (temp &shape)
{
shape.area();
temp shape_sample = shape;
shape_sample.area();
}
void fooWrapper(Shape* shape)
{
Rectangle* shape1 = dynamic_cast<Rectangle*>(shape);
if (shape1)
{
foo<Rectangle>(*shape1);
return;
}
Triangle* shape2 = dynamic_cast<Triangle*>(shape);
if(shape2)
{
foo<Triangle>(*shape2);
return;
}
shape->area();
return;
}
// Main function for the program
int main() {
unique_ptr<Shape> shape;
Triangle tri(10,7);
shape =make_unique<Triangle> (tri);
fooWrapper(shape.get());
Rectangle rec(10,7);
shape = make_unique<Rectangle> (rec);
fooWrapper(shape.get());
return 0;
}`
Please consider the following piece of code:
#include <iostream>
#include <string>
enum Type { T1, T2 };
class Base {
public:
std::string baseName;
Type type;
Base(const std::string& bn, Type t):
baseName(bn), type(t) {}
};
class Derived1 : public Base
{
public:
std::string dName;
int x = 10;
Derived1(const std::string& bn, const std::string& dn):
Base(bn, Type::T1), dName("Dervied1"+dn) {}
int getX(void) const { return x; }
};
class Derived2 : public Base
{
public:
std::string dName;
int y = 20;
Derived2(const std::string& bn, const std::string& dn):
Base(bn, Type::T2), dName("Derived2"+dn){}
int getY(void) const { return y; }
};
void func(Base& b)
{
if (b.type == Type::T1)
{
Derived1& d1 = static_cast<Derived1&>(b);
std::cout << d1.baseName << " " << d1.dName << " " << d1.getX();
std::cout << std::endl;
}
else
{
Derived2& d2 = static_cast<Derived2&>(b);
std::cout << d2.baseName << " " << d2.dName << " " << d2.getY();
}
};
int main(void)
{
Derived1 d1("Base", "foo");
func(d1);
Derived2 d2("Base", "foo");
func(d2);
}
The requirement is to have a function that can take in the base class value and then depending on the "type" of the derived instance, do something different. My question is - is this the right way of doing things or am I missing some important design pattern. I remember reading that use of static_cast or dynamic_cast means that there is something inherently wrong with the design. I understand that ideally the base class can have virtual functions that the derived classes implement, and at run time they get polymorphically dispatched. However, in this case there are two functions in each derived class that are specific to those classes, viz. getX and getY. How can I change the design to make it better and perhaps not use the cast?
Thanks!
The requirement is to have a function that can take in the base class value and then depending on the "type" of the derived instance, do something different.
That is exactly what polymorphism is all about. But you are not using it the way it is meant to be used.
My question is - is this the right way of doing things
No.
am I missing some important design pattern.
This would be better handled by getting rid of Type altogether and introduce a virtual method in Base.
I understand that ideally the base class can have virtual functions that the derived classes implement, and at run time they get polymorphically dispatched.
Exactly.
However, in this case there are two functions in each derived class that are specific to those classes, viz. getX and getY.
So? Using polymorphism correctly does not prevent that.
How can I change the design to make it better and perhaps not use the cast?
Use polymorphism correctly. For example:
#include <iostream>
#include <string>
class Base
{
public:
std::string baseName;
Base(const std::string& bn):
baseName(bn) {}
virtual void doIt() = 0;
};
class Derived1 : public Base
{
public:
std::string dName;
int x = 10;
Derived1(const std::string& bn, const std::string& dn):
Base(bn), dName("Dervied1"+dn) {}
int getX(void) const { return x; }
void doIt() override
{
std::cout << baseName << " " << dName << " " << getX();
std::cout << std::endl;
}
};
class Derived2 : public Base
{
public:
std::string dName;
int y = 20;
Derived2(const std::string& bn, const std::string& dn):
Base(bn), dName("Derived2"+dn) {}
int getY(void) const { return y; }
void doIt() override
{
std::cout << baseName << " " << dName << " " << getY();
}
};
void func(Base& b)
{
b.doIt();
}
int main(void)
{
Derived1 d1("Base", "foo");
func(d1);
Derived2 d2("Base", "foo");
func(d2);
}
And then take it a step farther by moving common code around so it can be shared by the derived classes:
#include <iostream>
#include <string>
class Base
{
public:
std::string baseName;
Base(const std::string& bn):
baseName(bn) {}
virtual void doIt()
{
std::cout << baseName;
}
};
class Derived : public Base
{
public:
std::string dName;
Derived(const std::string& bn, const std::string& dn):
Base(bn), dName(dn) {}
void doIt() override
{
Base::doIt();
std::cout << " " << dName;
}
};
class Derived1 : public Derived
{
public:
int x = 10;
Derived1(const std::string& bn, const std::string& dn):
Derived(bn, "Dervied1"+dn) {}
int getX(void) const { return x; }
void doIt() override
{
Derived::doIt();
std::cout << " " << getX();
std::cout << std::endl;
}
};
class Derived2 : public Derived
{
public:
int y = 20;
Derived2(const std::string& bn, const std::string& dn):
Derived(bn, "Derived2"+dn) {}
int getY(void) const { return y; }
void doIt() override
{
Derived::doIt();
std::cout << " " << getY();
}
};
void func(Base& b)
{
b.doIt();
}
int main(void)
{
Derived1 d1("Base", "foo");
func(d1);
Derived2 d2("Base", "foo");
func(d2);
}
If you have the option of using a virtual member function, as outlined by the other answer, it is the best approach to use. However, there are situations where you don't have that luxury. In that case, you can build your dispatch mechanism based on the type of the derived type.
#include <iostream>
#include <string>
#include <map>
class Base {
public:
std::string baseName;
Base(const std::string& bn): baseName(bn) {}
virtual ~Base() {}
// Don't store type ID per instance.
// Make it a virtual function so derived classes
// can return the same value for each instance.
virtual int getTypeID() = 0;
// Helper function for derived classes to use so each
// derived class can have a unique type id associated
// with it. This eliminates the need for having an enum.
static int getNextTypeID();
{
static int typeID = 0;
return ++typeID;
}
};
class Derived1 : public Base
{
public:
std::string dName;
int x = 10;
Derived1(const std::string& bn,
const std::string& dn):
Base(bn), dName("Dervied1"+dn) {}
// get type ID for this class.
// Every instance of the class will return
// same value.
virtual int getTypeID()
{
return getTypeIDStatic();
}
// This is a crucial piece of function
// that allows type based dispatch mechanism to work.
static int getTypeIDStatic()
{
static int typeID = Base::getNextTypeID();
return typeID;
}
int getX(void) const { return x; }
};
class Derived2 : public Base
{
public:
std::string dName;
int y = 20;
Derived2(const std::string& bn,
const std::string& dn):
Base(bn), dName("Derived2"+dn){}
int getY(void) const { return y; }
virtual int getTypeID()
{
return getTypeIDStatic();
}
static int getTypeIDStatic()
{
static int typeID = Base::getNextTypeID();
return typeID;
}
};
// Define a function type.
using Function = void (*)(Base& b);
// Keep a registry of functions that can be called for
// different types derived from Base.
std::map<int, Function>& getRegisteredFunctionsMap()
{
static std::map<int, Function> functionsMap;
return functionsMap;
}
// Provide a mechanism to register functions for types
// derived from Base.
template <typename T>
void registerFunction(Function f)
{
getRegisteredFunctionsMap()[T::getTypeIDStatic()] = f;
}
void func(Base& b)
{
// Check whether there is a function base on the type of b.
std::map<int, Function>& functionsMap = getRegisteredFunctionsMap();
std::map<int, Function>::iterator iter = functionsMap.find(b.getTypeID());
if ( iter != functionsMap.end() )
{
// If yes, call it.
iter->second(b);
}
else
{
// No function to deal with the type.
// Deal with the situation.
}
};
// A function that can be called when the real type is Derived1.
void derived1Fun(Base& b)
{
// Assume that b is derived.
Derived1& d1 = dynamic_cast<Derived1&>(b);
// Now use d1.
std::cout << d1.baseName << " " << d1.dName << " " << d1.getX();
std::cout << std::endl;
}
// A function that can be called when the real type is Derived2.
void derived2Fun(Base& b)
{
// Assume that b is Derived2.
Derived2& d2 = dynamic_cast<Derived2&>(b);
// Now use d2.
std::cout << d2.baseName << " " << d2.dName << " " << d2.getY();
std::cout << std::endl;
}
int main(void)
{
// Register functions for Derived1 and Derived2.
registerFunction<Derived1>(derived1Fun);
registerFunction<Derived2>(derived2Fun);
// Make the function calls.
Derived1 d1("Base", "foo");
func(d1);
Derived2 d2("Base", "foo");
func(d2);
}
Output of running the above program:
Base Dervied1foo 10
Base Derived2foo 20
In c++ pass to a function/method a derived class, in the place of a base class and still have information about the derived class?.
Ex: Say that I have a base class: "geometry" and some other class: "shape" form where I derive "rectangle" and "circle".
double area::geometry( shape& object, int i )
{
if i = 1:
rectangle thisObject = object;
double side1 = thisObject.getheight();
double side2 = thisObject.getwidth();
if i = 2:
circle thisObject = object;
double side1 = thisObject.radius * thisObject.radius;
double side2 = 3.14;
return side1 * side2;
}
The problem is that you have to suppose that the "return side1 * side2", is a very complicated code that I don't want to repeat. So I prefer to set up the problem depending on the type of input to the function, than to overload it.
The idea is very similar to this one: Is it possible to pass derived classes by reference to a function taking base class as a parameter.
Thanks!
Edit: Tried to make the code clearer.
The usual approach would be to use polymorphism:
void func(const base& ob)
{
ob.doSomethingPolymorphic();
}
where doSomethingPolymorphic() is a virtual member function of base.
If func needs to know what type ob is when you pass it in, "you are doing it wrong". The WHOLE POINT of polymorphism is that all objects appear to be the same from an outside observer, but internally do different things. The typical example is animals:
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void Say() = 0;
};
class Dog : public Animal
{
public:
void Say() { cout << "Woof Woof" << endl; }
};
class Pig : public Animal
{
public:
void Say() { cout << "All animals are equal, but pigs are "
"more equal than other animals" << endl; }
};
class Cat : public Animal
{
public:
void Say() { cout << "Meow, Meow" << endl; };
};
void AnimalTalk(Animal *a[], int size)
{
for(int i = 0; i < size; i++)
{
a[i]->Say();
}
}
int main()
{
Cat c;
Pig p;
Dog d;
Animal* list[3] = { &p, &c, &d };
AnimalTalk(list, 3);
}