Hi Guys,

In my recent thread on a polymorphism problem I thought I had it all figured out but now I have I new problem.

Now I want to pass in 2 objects of either type bird or pigeon

int main()
{
Bird B1;
Pigeon P1;

DoSomething ( &B1, &B2 );
DoSomething ( &P1, &P2 );
}


void DoSomething(*ptr1, *ptr2){
   ptr1->methodA(*ptr2)      
         
//Where methodA will return an object of Type Bird or 
//Pigeon depending on what *ptr1 and *ptr2 are pointing
//to.  As methodA is a virtual function and has been  
//redefined for Pigeon.
}

What I want to do is print out the data held in the object returned by methodA but I cannot say myResult = ptr1->methodA(*ptr2) and print out myResult as I do not yet know the type of myResult.
Is there anyway I can achieve this?

Any ideas welcome,
Thanks.

Recommended Answers

All 5 Replies

so i am assuming that Pigeon is a subclass of Bird?

I am not sure what methodA does; but it should probably return a Bird *, since that works for both Birds and Pigeons.

the print() method should be virtual in Bird, if you want it to be able to act differently for Pigeons

Pigeon should override those methods, so it can do something different

class Bird {
  virtual Bird *methodA(Bird *x);
  virtual void print();
}

class Pigeon : publid Bird {
  Bird *methodA(Bird *x);
  void print();
}

void DoSomething(Bird *ptr1, Bird *ptr2){
   Bird *myResult = ptr1->methodA(ptr2);
   myResult->print();
}

Hi Bugmenot,
Thanks for the help,
Yes maybe this might be a better way of doing it. Currently I am returning an object of type bird or pigeon depending on the virtual method called.

Yes, Pigeon is a subclass of Bird.
I will try and return pointers instead from these methods and go from there.
I have already defined my methodA as virtual and have 2 different versions, one for bird and one for pigeon.
I think returning pointers instead of an object of bird or pigeon would be better.
Thanks,I'll try this out.

Hi again,
I tried this example and can get it to work for my scenario,

#include <iostream>
  class Bird {
      public:
      virtual void Amethod() {
      std::cout<<"Amethod from Bird\n";
      }
};
      class Pigeon: public Bird {
      virtual void Amethod() {
      std::cout<<"Amethod from Pigeon\n";
      }

};
      void DoSomething ( Bird *p )
      {
      p->Amethod();
      }
      int main()
      {
      Bird B1;
      Pigeon P1;
      DoSomething ( &B1 );
      DoSomething ( &P1 );
      }

however, I would like it do the following:

#include <iostream>

class Bird {

	public:
		virtual void Amethod(Bird test){
			std::cout << "A method from Bird";
			}
};

class Pigeon: public Bird {
	virtual void Amethod(Pigeon test2){
		std::cout << "A method from Pigeon";
		}
};


void DoSomething (Bird *p, Bird *p2)
	{
	p->Amethod(*p2);

	}

int main()
	{
	Bird B1,B2;
	Pigeon P1,P2;

	DoSomething(&B1,&B2);   //want to call the bird method
	DoSomething(&P1,&P2);   //want to call the pigeon method here!!!
	}

However when I try this, the Method from Bird is always called.
Does anybody know how I can solve this?
Thanks.

to override Amethod in Pigeon, it will have to be declared the same way as in Bird

virtual void Amethod(Bird *test2)

is Pigeon.Amethod() always going to be called on a Pigeon? What if it is called on a Bird?

You could just try to cast the Bird to a Pigeon.

P.S. I noticed that you didn't use pointers in the argument to Amethod. If you want to be able to take multiple types, you are going to have to take a pointer or reference as the argument.

Yes, Pigeon.Amethod() is always going to be called on a Pigeon. Will I still have to pass a pointer in the argument list even if this is the case?
Is it just enough to pass in *test to the method, Amethod currently returns an object of either type pigeon or bird(depending which object invoked the method, a bird or pigeon).
Kind of getting lost in a sea of pointers...not my strongest point!!:-)
Any ideas on how I would set up these pointers??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.