通过const限定符(成员函数指针的)重载分辨率? [英] Overload resolution by const qualifier (of member function pointer) ?
问题描述
当我仅在参数的const限定符上重载类的成员函数时(这是不同类的成员函数指针),编译器将解决重载并按预期执行.
当我为非成员函数尝试相同的重载时,编译器(VS2010 Express)抱怨链接器错误.
有谁知道为什么吗?
When I overload a class''s member function solely on the const qualifier of the parameter (which is a different class''s member function pointer), the compiler resolves the overload and performs as expected.
When I attempt the same overload for a non-member function, the compiler (VS2010 Express) complains of linker errors.
Does anyone know why ?
//Header.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
struct Component
{
int Execute(bool X) { return 0; }
int ConstExecute(bool X) const { return 0; }
};
struct Tester
{
void Con( int (Component::*MFP)(bool) )
{ std::cout << "Tester::Con Passed Non-Const MFP\n"; };
void Con( int (Component::*MFP)(bool) const )
{ std::cout << "Tester::Con Passed Const MFP\n"; };
};
void Con( int (Component::*MFP)(bool) )
{ std::cout << "Con Passed Non-Const MFP\n"; };
void Con( int (Component::*MFP)(bool) const )
{ std::cout << "Con Passed Const MFP\n"; };
//Main.cpp
#include "Header.h"
int _tmain(int argc, _TCHAR* argv[])
{
// In Main
Tester MyTester;
MyTester.Con( &Component::Execute );
MyTester.Con( &Component::ConstExecute );
Con( &Component::Execute );
// LNK2005: Con(int (__thiscall Component::*)(bool) already defined
Con( &Component::ConstExecute );
// LNK2005: Con(int (__thiscall Component::*)(bool)const ) already defined
char X;
std::cin >> X;
return 0;
}
注释掉对非成员Con函数的调用会产生:
"Tester :: Con通过了非常量MFP"
"Tester :: Con通过了Const MFP"
符合预期.
Commenting out the calls to the non-member Con Function produces:
"Tester::Con Passed Non-Const MFP"
"Tester::Con Passed Const MFP"
as expected.
推荐答案
忽略我的评论.我不是在想什么.
问题在于此代码是在头文件中定义的.
这意味着,对于每个包含此文件的.cpp文件,都将定义功能代码(使用相同的名称).
#pragma once
仅停止此文件被任何1个cpp文件包含多次,它不会阻止其包含我的多个文件.
在标头中给出函数原型,或者仅在1个cpp文件中定义代码,或者在函数的开头添加inline
关键字
Ignore my comment. I wasn''t thinking straight.
The issue is that this code is defined in a header file.
This means that for every .cpp file that includes this, the function code is defined (under the same name).
The#pragma once
only stops this file getting included more than once by any 1 cpp file, it doesn''t stop it getting included my multiple files.
Either give the function prototype in the header, and define the code in just 1 cpp file, or add theinline
keyword to the start of the functions
inline void Con( int (Component::*MFP)(bool) )
{ std::cout << "Con Passed Non-Const MFP\n"; };
inline void Con( int (Component::*MFP)(bool) const )
{ std::cout << "Con Passed Const MFP\n"; };
inline关键字使编译器本质上可以在调用函数的任何地方复制并粘贴函数代码,而无需定义实际函数.这有点麻烦,但是可以.
The inline keyword makes the compiler essentially copy and paste the function code everywhere the function is called, without defining an actual function. This is a bit of a hack up, but works.
定义非成员函数const
时没有内在的含义.在类中,声明为const
的成员函数意味着它不会更改您调用它的类的实例.对于非成员函数,这没有任何意义!没有与非成员functino相关的类的实例",因此const
限定词是不可能的.恕我直言,编译器(而不是链接器!)甚至应该发出错误.
您可以根据函数参数的常数进行重载.
简而言之,您不能通过将const限定符添加到功能,而仅通过将它们添加到参数来重载非成员函数. br/>
附言:请忽略这一点,这只是我的阅读技巧不佳的结果...
There is no inherent meaning when you define a non-member functionconst
. In a class, a member function that is declaredconst
means that it won''t change the instance of the class you call it on. For a non-member function that makes no sense however! There is no ''instance'' of a class that a non-member functino relates to, therefore aconst
qualifier is not possible. IMHO the compiler (not the linker!) should even issue an error.
You can make an overload based on the constness of a function''s parameters though.
In short, you can not overload non-member functions by adding a const qualifier to the function, only by adding them to the parameters.
P.S.: ignore this, it''s just a result of my bad reading skills...
这篇关于通过const限定符(成员函数指针的)重载分辨率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!