重载类成员函数 [英] Overloading Class Member Function
问题描述
我已经将一个函数 fn
重载为 fn(int,char)
&fn(int&,char&)
如下图:
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;
}
调用 fn(int,char)
或 fn(int&,char&)
的解析是通过 cast static_cast<void(*)(int&,char&)>(fn)(a, c);
The resolution for call to fn(int,char)
or fn(int&,char&)
is made through cast static_cast<void(*) (int&, char&)> (fn)(a, c);
它给出输出:
$ ./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;
}
我收到以下错误:
$ 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()’
我该如何解决这个问题?如何正确调用 T 的 fn(int&,char&)
How do I resolve this?
How to make a proper call for T's fn(int&,char&)
我猜在我的代码中表达式 static_cast
不正确.
I guess in my code the expression static_cast<void(*) (int&, char&)> (T.fn)(a, c);
is incorrect.
请帮忙.
谢谢
我的错误
将Test T()
编辑为Test T;
报错
$ 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&)’
推荐答案
第一:T
不是变量,T
是函数,返回Test代码>并没有收到任何东西.
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);
这篇关于重载类成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!