In the following code, f1 is an overloading function. In what situation will be the first called, in what situation will the second be called?

#include <iostream>
using namespace std;

class A
{
public:
    void f1(){cout<<"f1 "<<endl;}
    void f1() const {cout<<"f1 "<<endl;}
};

int main()
{
    A a;
    a.f1();
    return 0;
}

Recommended Answers

All 8 Replies

Who cares? It's a mistake to write such confusing code anyway.

A c++ compiler will produce errors on that code because in order to overload a function the parameters must be different. In the code you posted both versions of the function have the same identical parameters. So your question is moot because that code can not be compiled.

A c++ compiler will produce errors on that code because in order to overload a function the parameters must be different. In the code you posted both versions of the function have the same identical parameters. So your question is moot because that code can not be compiled.

Actually this compiled and ran for me using Dev C++. I changed the cout display to make it more clear which function is called, but otherwise it's the same. The non-const function gets called.

#include <iostream>
using namespace std;

class A
{
    public:
        void f1()
        {
            cout<<"f1 non-const function"<<endl;
        }
        void f1() const 
        {
             cout<<"f1 const function"<<endl;
        }
};

int main()
{
    A a;
    a.f1();
    return 0;
}

Follow up. Adding a constuctor and comparing an instance of A with and without the const modifier, I ran the below program. The instance of A with the const modifier called the f1 function with the const modifier. The instance of A without the const modifier called the f1 function without the const modifier:

#include <iostream>
using namespace std;

class A
{
      public:
        void f1() const
        {
             cout<<"f1 const function"<<endl;
        }
        
        
        void f1()
        {
            cout<<"f1 non-const function"<<endl;
        }
        
        
        A(int anInt)
        {
            i = anInt;
        }
        
        
        int i;
};

int main()
{
    const A a(8);
    a.f1();
    A b(9);
    b.f1();
    return 0;
}

Program results:

f1 const function
f1 non-const function

commented: Great +1
commented: Interesting :) +2

You're right -- I forgot about the const modifyer satisfying the overload conditions.

Thank you Vernon!

i have this overload code when i was a college freshman...
try this out and maybe it can give you hints on what to do...

#include<iostream>
using namespace std;
float trans(char str, int i);
float trans(char str, double d);
float trans(char str, long L);

int main()
{
	int i;
	double d;
	long L;
	float ans;
	
	trans("Enter an integer: ",&i);
	trans("Enter a double: ",&d);
	trans("Enter a long: ",&L);
	return 0;
}//end main

float trans(char str, int i,float ans)
{
	cout<<str;
	cin>>i;
	ans=i*1+32;
	return (ans);
}

float trans(char str, double d,float ans)
{
	cout<<str;
	cin>>d;
	ans=d*1+32;
	return (ans);
}

a cv-qualifier suffix following the (non-static) member function's parameter list is a specifier for the this pointer which is an implicit parameter to the function. overloading on cv-qualifiers is overloading on the type of the this pointer; and normal overloading resolution applies. eg.

#include <iostream>
#include <typeinfo>

struct some_class
{
  void foo( int a )
  { std::cout << "type of this: " << typeid(this).name() << '\n' ; }
  void foo( int a ) const
  { std::cout << "type of this: " << typeid(this).name() << '\n' ; }
  void foo( int a ) volatile // esoteric
  { std::cout << "type of this: " << typeid(this).name() << '\n' ; }
};

int main()
{
  some_class a ;
  const some_class& ca = a ;
  volatile some_class& va = a ;

  a.foo(100) ;
  ca.foo(100) ;
  va.foo(100) ;
}

overloading on the const specifier is done for reasons of const-correctness. for example, begin() is overloaded on the const specifier by standard containers.
see: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.12

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.