-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.

Community
  • 1
  • 1
Alejandro
  • 21
  • 2
  • 1
    I sense an XZ problem... – Kerrek SB Sep 10 '13 at 00: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? ;) – Mats Petersson Sep 10 '13 at 00: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! – Alejandro Sep 10 '13 at 00:56

2 Answers2

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • 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 Sep 10 '13 at 00:59
  • @Alejandro so you call `ob.area();`. – juanchopanza Sep 10 '13 at 06:07
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);
}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 1
    You cannot have an array of references, nor a pointer to a reference. – Kerrek SB Sep 10 '13 at 00:26
  • @KerrekSB: I thought it looks a bit suspicious, so I have compiled and fixed the code... – Mats Petersson Sep 10 '13 at 00:31
  • @MatsPetersson Is it a subtle trolling or you just copy-pasted it wrongly while editing? ;) – lapk Sep 10 '13 at 00:37
  • 1
    @PetrBudnik: I must have made a mistake in copy'n'paste... Should be OK now. – Mats Petersson Sep 10 '13 at 00:41
  • Thanks. Now I can +1 it too. Just because I also happen to think that pigs are more equal than other animals. – lapk Sep 10 '13 at 00:47
  • Thank you!, Yes, but the point is that the function say, is not quite a function, is a method in another class that is working with animals ( say farmers ). That is why I want the farmer to say: func=capitalize each animal, depending on the animal. (( this example was inspired by your code :) )) thanks!. – Alejandro Sep 10 '13 at 01:02
  • Not sure what you are saying. Can you please edit your code with a more realistic example? I'm pretty sure we can find a solution, but to find the right solution, it's definitely helpful to more fully understand what you are trying to do. – Mats Petersson Sep 10 '13 at 01:07
  • Thank you very much, I did a edit to the code, please tell me if it is clearer. – Alejandro Sep 10 '13 at 01:37