Polymorphism (function overloading)

Hi,

I tried, but i can't change my way of thought of coding, so i deadly need help from you.

I'm trying to overload a pure virtual function defined in base class in my derived class. But the problem is that I want the function to take an argument that is an "object" of type "derived" class.

1- Is it possible to overload a function that takes an "object" as an argument?
2- If yes, what should i write in function definitions as arguments? (maybe each takes an "object" of type "corresponding class? or smt else?)

Thanks for reading and helps.

Can you give an example (using code) of what you are trying to do here?

The prototypes must match
1- Is it possible to overload a function that takes an "object" as an argument?
Yes. All the fundamental types are objects. For example, "void foo(int x)", then i can overload it with "void foo(float x)".

2.f yes, what should i write in function definitions as arguments? (maybe each takes an "object" of type "corresponding class? or smt else?)
Writing another function with the same name in the derived class is actually called "overriding", not overloading, and they have to have the same arguments and the same output as the ones in the base class( the output rule is relaxed by using the covariant return types).To overload the function, you have to write all the versions of that function in the base class. If you overload it in the base class, then in the derived class, you'll have to redefine all these overloaded functions again.
What you can do is using either reference or pointer to the base class as your function argument.
1
2
3
4
5
6
7
8
9
10
11
12
class Base
{
  // this is overloading
  virtual int foo(Base * x) { return 1;} 
  virtual Base * foo(Base *z , int x) { return this}; 
}
class Derived
{
   // redefine the virtual functions in derived class is overriding. 
   void foo(Base *x);
   void Derived* foo(Base *z, int x){return this}; // covariant return type. 
}


Last edited on
You are not overriding in the base class. You are defining new overloaded functions. The comments are correct.
Thank you all for your precious answers.

I can't believe that I'd "overlooked" an other silly mistake while focusing on that issue.
Everything i did was right. Besides, you are righter :)
Topic archived. No new replies allowed.