Gaurav K
Gaurav K

Reputation: 2975

Overloading Class Member Function

I have overloaded a function fn as fn(int,char) & fn(int&,char&) as shown below:

#include <iostream>

using namespace std;


void fn(int a, char c);
void fn(int& a, char& c);

int main()
{
   int a=10;
   char c= 'c';



  cout << "Inside main()" << endl;
  cout << hex << "&a : " << &a << endl;
  cout << hex << "&c : " << (int *)&c << endl;

   static_cast<void(*) (int&, char&)> (fn)(a, c);

    return 0;
    }


void fn(int a, char c)
{
    int tempInt;
    char tempChar;
    cout << "\n\nInside Call By Value Function " << endl;
    cout << hex << "&a : " << &a << endl;
    cout << hex << "&c : " << (int *)&c << endl;
    cout << hex << "&tempInt : " << &tempInt << endl;
    cout << hex << "&tempChar : " << (int *)&tempChar << endl;
    }


void fn(int& a, char& c)
{

    cout << "\n\nInside Call By Reference Function " << endl;
    cout << hex << "*a : " << &a << endl;
    cout << hex << "*c : " << (int*) &c << endl;

    }

The resolution for call to fn(int,char) or fn(int&,char&) is made through cast static_cast<void(*) (int&, char&)> (fn)(a, c);

It gives output:

$ ./Overloading
Inside main()
&a : 0x22ac5c
&c : 0x22ac5b


Inside Call By Reference Function
*a : 0x22ac5c
*c : 0x22ac5b

Now when I put this in a class as below:

#include <iostream>

using namespace std;

class Test{
public:
void fn(int a, char c);
void fn(int& a, char& c);
};



int main()
{
   int a=10;
   char c= 'c';

  Test T();
  cout << "Inside main()" << endl;
  cout << hex << "&a : " << &a << endl;
  cout << hex << "&c : " << (int *)&c << endl;

   static_cast<void(*) (int&, char&)> (T.fn)(a, c);

    return 0;
    }


void Test::fn(int a, char c)
{
    int tempInt;
    char tempChar;
    cout << "\n\nInside Call By Value Function " << endl;
    cout << hex << "&a : " << &a << endl;
    cout << hex << "&c : " << (int *)&c << endl;
    cout << hex << "&tempInt : " << &tempInt << endl;
    cout << hex << "&tempChar : " << (int *)&tempChar << endl;
    }


void Test::fn(int& a, char& c)
{

    cout << "\n\nInside Call By Reference Function " << endl;
    cout << hex << "*a : " << &a << endl;
    cout << hex << "*c : " << (int*) &c << endl;

    }

I get below error:

$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:42: error: request for member ‘fn’ in ‘T’, which is of non-class type ‘Test()’

How do I resolve this? How to make a proper call for T's fn(int&,char&)

I guess in my code the expression static_cast<void(*) (int&, char&)> (T.fn)(a, c); is incorrect.

Please help.

Thanks

EDIT:

My mistake

editing Test T() to Test T;

gives error

$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:44: error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(int&, char&)’

Upvotes: 0

Views: 1260

Answers (3)

ForEveR
ForEveR

Reputation: 55887

First: T is no variable, T is function, that returns Test and receive nothing.

Second: function-pointer is not member-function-pointer. You should use this syntax

typedef void (Test::*function)(int&, char&);
function f = &Test::fn;
(T.*f)(a, c);

Upvotes: 2

Kakalokia
Kakalokia

Reputation: 3191

When you do Test T();, you're saying that T is a function with return type Test. However no such thing exists in your code.

The solution is:

Test T;

Upvotes: 0

Joachim W
Joachim W

Reputation: 8167

See error: request for member '..' in '..' which is of non-class type: the problem is with Test T();, try ommiting the parentheses.

Upvotes: 0

Related Questions