std::wstring和LOGFONT重载冲突-为什么? 我在GCC 4.81.下的C++项目中工作。我有两个getter/setter对: LOGFONT GetTitleBarFont(); void SetTitleBarFont(LOGFONT titleBarFont); std::wstring GetTitleBarFont(); void SetTitleBarFont(std::wstring titleBarFont);

std::wstring和LOGFONT重载冲突-为什么? 我在GCC 4.81.下的C++项目中工作。我有两个getter/setter对: LOGFONT GetTitleBarFont(); void SetTitleBarFont(LOGFONT titleBarFont); std::wstring GetTitleBarFont(); void SetTitleBarFont(std::wstring titleBarFont);,c++,gcc,overloading,C++,Gcc,Overloading,但出于某种原因,GCC告诉我这些重载无效 error: 'std::wstring GetTitleBarFont()' cannot be overloaded error: with 'LOGFONT GetTitleBarFont()' 我不明白这里的问题是什么std::wstring是一种STL类型(std::basic_string),在后台有大量模板工作 LogFoo> 是Windows数据类型(),几乎完全由本机C++数据类型组成(长< /Calp> S,和字节< /Cult>

但出于某种原因,GCC告诉我这些重载无效

error: 'std::wstring GetTitleBarFont()' cannot be overloaded
error: with 'LOGFONT GetTitleBarFont()'

我不明白这里的问题是什么
std::wstring
是一种STL类型(
std::basic_string
),在后台有大量模板工作<代码> LogFoo> <代码>是Windows数据类型(),几乎完全由本机C++数据类型组成(<代码>长< /Calp> S,和<代码>字节< /Cult> s,其中一个古怪的代码<代码> TCHAR 数组。这些重载怎么可能是不明确的?

您不能基于返回类型重载方法,因为重载解析考虑了函数签名

1.3.11签名

有关参与重载的函数的信息 分辨率(13.3):其参数类型列表(8.3.5),如果 函数是类成员,函数上的cv限定符(如果有) 本身和声明成员函数的类。[……]

---编辑---
要详细说明可能的解决方案,您可以

1) 更改getter的名称:

std::wstring GetTitleBarFontWString();
LOGFONT GetTitleBarFontLogFont();
2) 没有参数(这并不能很好地组合,但有时您必须这样做)

3) 滥用模板专门化,以便调用方可以指定他想要返回的内容。(
GetTitleBarFont
GetTitleBarFont

基本上没有真正好的解决方案


chris获得了3分的全部学分,2分的部分学分)

假设字符串返回函数不提供字体,而是提供字体名称,那么显而易见的解决方案是

std::wstring GetTitleBarFontName();
void SetTitleBarFontName(std::wstring titleBarFont);

因为您不能仅使用返回类型作为鉴别器来重载。

对于遇到此问题并感到奇怪的任何人,就像我刚才所做的那样,为什么C++在确定使用哪一个过载时不考虑返回值:对这个情况的一个非常详细的答案。

不能在返回类型上超载。或者,用户指定类型:<代码> GETTITLE BARFONTUN()/<代码>代码> GETTITLE BARFONTUN()/CODE >,或者使用引用参数:<代码> GETTILE栏(someLOGFONT)或
GetTitleBar(someWstring)
@Xam:基本上没有真正好的解决方案-是的,有:正确命名函数。它不返回字体,但它的名称,命名它
Get/SetTitleBarFontName
没有问题:)@JohannGerell详细信息…;-)假设他出于任何原因想要保持名称不变(例如,他想调用GetTitleBarFront并获得他想要的返回类型的一些模板魔术),那么没有,但是是的,重命名名称getter以获得名称是有意义的。
std::wstring GetTitleBarFontName();
void SetTitleBarFontName(std::wstring titleBarFont);