saz*_*azr 2 c++ syntax overloading namespaces
如果函数在命名空间中声明,如何才能正确使用这个'+ ='函数?
namespace WinFile
{
std::stack <tstring> operator += ( std::stack <tstring>& s1, std::stack <tstring> s2 )
{
// Post:
if ( s1 == s2 )
{
return s1;
}
while ( !s2.empty() )
{
s1.push( s2.top() );
s2.pop();
}
return s1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我如何使用此函数(不说使用命名空间WinFile):
std::stack <tstring> s1;
std::stack <tstring> s2;
// ...after adding some values to the stacks
s1 += s2; // this gets a compile error
s1 WinFile::+= s2 // this says its invalid to have a ':' infront of a +=
Run Code Online (Sandbox Code Playgroud)
如前所述,您可以使用"using"子句:
using WinFile::operator+=;
Run Code Online (Sandbox Code Playgroud)
要么
using namespace WinFile;
Run Code Online (Sandbox Code Playgroud)
或者您可以使用以下代码直接使用该函数:
s1 = WinFile::operator +=( s1, s2 );
Run Code Online (Sandbox Code Playgroud)
其中没有一个是特别理想的,但据我所知,没有其他方法可以做到这一点.
| 归档时间: |
|
| 查看次数: |
168 次 |
| 最近记录: |