1

Can we achieve polymorphism through const function? I mean does a function a() and another function a()const behave polymorphically?

void func(int a){}
void func(int a)const {}
Cory Klein
  • 51,188
  • 43
  • 183
  • 243
Bhupesh Pant
  • 4,053
  • 5
  • 45
  • 70
  • No, they must have the same signature. – chris Jul 11 '13 at 18:53
  • 2
    That depends what you mean by polymorphism. Overloading, or [Ad hoc polymorphism](http://en.wikipedia.org/wiki/Ad-hoc_polymorphism), is a type of polymorphism. However when people talk about polymorphism in C++ they usually mean dynamic dispatch via virtual functions (also known as [subtype polymorphism](http://en.wikipedia.org/wiki/Subtype_polymorphism)), which this is not. – David Brown Jul 11 '13 at 19:01
  • I think you need to explain what you mean by polymorphism, because the answer could go either way. – juanchopanza Jul 11 '13 at 19:02
  • Needs to be virtual for polymorphism. You can check if this is the case using the override keyword in C++11 – Justin Meiners Jul 11 '13 at 19:02
  • 1
    @JustinMeiners needs to be virtual for *virtual* or runtime polymorphism. But there are more types of polymorphism. Function overloading provides another means for polymorphism. – juanchopanza Jul 11 '13 at 19:06
  • @juanchopanza How in C++ could a method be called polymorphicaly without virtual? (genuine question not smart ass) – Justin Meiners Jul 11 '13 at 19:09
  • @juanchopanza ah I now see from your edit what your talking about – Justin Meiners Jul 11 '13 at 19:11
  • @JustinMeiners Compile time polymorphism: `void foo(int)`, `void foo(std::string)`. Or, following the example above, `void foo(someType&)`, `void foo(const someType&)`. – juanchopanza Jul 11 '13 at 19:12
  • [What is compile-time polymorphism](http://stackoverflow.com/questions/1881468/c-what-is-compile-time-polymorphism-and-why-does-it-only-apply-to-functions) – Mooing Duck Jul 11 '13 at 19:54

4 Answers4

6

Answer is . you can overload it !!

#include<iostream>
using namespace std;

class Test
{
protected:
    int x;
public:
    Test (int i):x(i) { }
    void fun() const
    {
        cout << "fun() const called " << endl;
    }
    void fun()
    {
        cout << "fun() called " << endl;
    }
};

int main()
{
    Test t1 (10);
    const Test t2 (20);
    t1.fun();
    t2.fun();
    return 0;
}

Output: The above program compiles and runs fine, and produces following output.

fun() called
fun() const called

The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object. C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer.

S J
  • 3,210
  • 1
  • 22
  • 32
3

This is not polymorphism. This is function overloading.

You might be confusing overloading with overriding.

Function overloads are two functions that have the same name, but different signatures. The code you've posted is an example. The parts of a function that make up the signature include the parameter list and the const qualification of the method. Notably, the function's return type is not part of the function's signature.

Function overrides are created using the keyword virtual. The have the same name and the same* signature. The basic idea behind overrides is to be able to call a derived class' special functions if all you have is the pointer to the base class. Here's an example:

class Foo
{
public: 
  virtual std::string GetIt() const { return "FOO" }
};

class Bar : public Foo
{
public:
  std::string GetIt() const { return "BAR"; }
};

int main()
{
  Foo* that = new Bar;
  cout << that->GetIt();
}

The program will output "BAR", even though the pointer is a pointer to a Foo.


*"the same signature": Actually, co-variant signatures.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • 3
    Function overloading is a type of polymorphism. – David Brown Jul 11 '13 at 19:05
  • @DavidBrown: Perhaps in an academic sense, but the Standard is quite clear what "polymorphism" means in C++: **10.3 Virtual Functions** *1/Virtual functions support dynamic binding and object-oriented programming. A class that declares or inherits a virtual function is called a polymorphic class.* – John Dibling Jul 11 '13 at 19:10
  • A class that inherits a virtual function _is_ a polymorphic class. Does the Standard also imply that overloading/templates are _not_ polymorphic? – Mooing Duck Jul 11 '13 at 19:56
  • @MooingDuck: The standard doesn't say that overloads & templates *are* polymorphic. Hence they aren't according to the Standard, in much the same way that `std::vector` is not part of the STL. – John Dibling Jul 11 '13 at 19:58
  • So maybe there are "polymorphic classes", but the term "polymorphic" isn't restricted to that. There are also *polymorphic function wrappers* in §20.8.11. – Xeo Jul 11 '13 at 20:09
  • I am bit confused here, what I know is that overloading are compile time polymorphism and overriding is runtime polymorphism. – Bhupesh Pant Jul 12 '13 at 01:43
1

As @chris explained, "No, they must have the same signature".

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
0

Yes we can achieve polymorphic behavior.

As we can able to call those functions, one(const function) with constant object and the other(non constant function) with non constant object. Both the functions have same name but they are of different usage nd polymorphism also refers to same name but different forms.

Raju
  • 1,149
  • 1
  • 6
  • 19