小编ehr*_*emo的帖子

返回值和局部变量之间的差异

假设我有

#include <string>

class A
{
public:
    template<class T>
    operator T();

    A child();
};

void f()
{
    A a;
    std::string s1 = a;            // ok
    std::string s2 = a.child();    // error (line 34)

    s1 = a;           // error (line 36)
    s2 = a.child();   // error (line 37)

}
Run Code Online (Sandbox Code Playgroud)

std :: string构造函数可以使用char*或std :: string引用,这就是赋值不明确的原因.但是为什么我的编译器(VC++ 10)抱怨第二个赋值而不是第一个赋值?

我正在寻找一种方法来优先使用模板转换运算符而不是重载的构造函数.

我收到以下错误:

1>------ Build started: Project: Plasma4Test, Configuration: Debug Win32 ------
1>  Plasma4Test.cpp
1>d:\bitbucket\vx\projects\plasma4test\plasma4test.cpp(34): error C2440: 'initializing' : cannot convert from 'A' to 'std::basic_string<_Elem,_Traits,_Ax>'
1>          with …
Run Code Online (Sandbox Code Playgroud)

c++ constructor return-value local-variables conversion-operator

7
推荐指数
1
解决办法
276
查看次数

用c ++中的字符串索引的整数

可能重复:
在C数组中为什么这是真的?a [5] == 5 [a]

这怎么可能是有效的C++?

void main()
{
  int x = 1["WTF?"];
}
Run Code Online (Sandbox Code Playgroud)

在VC++ 10上,这个编译并在调试模式下x,语句之后的值为84.

这是怎么回事?

c++ string indexing int integer

5
推荐指数
2
解决办法
191
查看次数