-2

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.

3
  • 1
    I sense an XZ problem... Commented Sep 10, 2013 at 0:11
  • 1
    @KerrekSB: Is an XZ problem one where you have a problem Z, to which you think the solution is Y, so therefore you ask how to do X which has nothing to do with Y? ;) Commented Sep 10, 2013 at 0:16
  • The question hopefully is clear: I don't want to overload the function because the code is practically the same. I gave a partial solution because I though it could be solved that way, that is why it might be XZ problem. Thank you for your answer! Commented Sep 10, 2013 at 0:56

2 Answers 2

3

The usual approach would be to use polymorphism:

void func(const base& ob) { ob.doSomethingPolymorphic(); } 

where doSomethingPolymorphic() is a virtual member function of base.

Sign up to request clarification or add additional context in comments.

2 Comments

I see, the issue is that ob.doSomethingPolymorphic() would relate to the different classes, while it is really the func the one that is doing something different for each of the derived classes. Say: func = area is a method in geometry, that is being applied to derived classes: "triangles and circles" in class shapes so I want area(shapes) to calculate the area of triangles and circles.
@Alejandro so you call ob.area();.
3

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); } 

8 Comments

You cannot have an array of references, nor a pointer to a reference.
@KerrekSB: I thought it looks a bit suspicious, so I have compiled and fixed the code...
@MatsPetersson Is it a subtle trolling or you just copy-pasted it wrongly while editing? ;)
@PetrBudnik: I must have made a mistake in copy'n'paste... Should be OK now.
Thanks. Now I can +1 it too. Just because I also happen to think that pigs are more equal than other animals.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.