看起来很奇怪。在这里您可以看到错误消息是一种类型之间发生转换并且失败。如果我从 Vector3 的复制构造函数中删除显式修饰符,那就很好,没有错误。有人可以解释为什么吗?我很困惑。
template<typename T>
class Vector3 {
public:
explicit Vector3(const Vector3& v) :x(v.x), y(v.y), z(v.z) {}
Vector3() :x(0), y(0), z(0) {}
T x, y, z;
};
template<typename T>
Vector3<T> getVec3() {
return Vector3<T>(); //c2440 "return":cannot convert Vector3<int> to Vector3<int>
}
int main()
{
getVec3<int>();
}
Run Code Online (Sandbox Code Playgroud) 我遇到了类的问题,将一个const对象(多态结构)传递给一个显式构造函数,该构造函数对该多态结构的基类进行了const引用.这是示例(这不是我的代码,这里是解释)
class Base
{
...
}
class Derived:public Base
{
...
}
class Problem
{
Problem(const Base&);
...
}
void myFunction(const Problem& problem)
{
...
}
int main()
{
//explicit constructor with non const object
Derived d;
Problem no1(d); //this is working fine
myFunction(no1);
//implicit constructor with const object
Problem no2=Derived(); //this is working fine, debugged and everything called fine
myFunction(no2); //is working fine
//explicit constructor with const object NOT WORKING
Problem no3(Derived()); //debugger jumps over this line (no compiler …Run Code Online (Sandbox Code Playgroud) class Base
{
}
class Derived1 : Base
{
}
class Derived2 : Base
{
public static explicit operator Derived1(Derived2 d2)
{
return new Derived1();
}
}
class Test
{
static void Main()
{
Base bd2 = new Derived2();
Derived1 d2ConvertedD1 = (Derived1)bd2; //throws InvalidCastException
}
}
Run Code Online (Sandbox Code Playgroud)
Unable to cast object of type 'ConsoleApplication1.Derived2' to type 'ConsoleApplication1.Derived1'.
为什么?我的运营商转换有什么问题?
我从未对重载运算符做过任何大量的工作,尤其是隐式和显式转换.
但是,我有几个频繁使用的数字参数,所以我创建一个struct作为数字类型的包装,以强类型这些参数.这是一个示例实现:
public struct Parameter
{
private Byte _value;
public Byte Value { get { return _value; } }
public Parameter(Byte value)
{
_value = value;
}
// other methods (GetHashCode, Equals, ToString, etc)
public static implicit operator Byte(Parameter value)
{
return value._value;
}
public static implicit operator Parameter(Byte value)
{
return new Parameter(value);
}
public static explicit operator Int16(Parameter value)
{
return value._value;
}
public static explicit operator Parameter(Int16 value)
{
return new Parameter((Byte)value);
}
}
Run Code Online (Sandbox Code Playgroud)
当我试验我的测试实现以获得显式和隐式运算符的挂起时,我试图显式地转换Int64为我的 …
不确定这是否是一个多余的问题,但请考虑我有这些方法:
void Foo(SomeClass x)
{
//Some code
}
void Foo(AnotherClass x)
{
//Some code
}
Run Code Online (Sandbox Code Playgroud)
让我们说我想用null调用一个特定的重载(SomeClass一),这是我的选择:
Foo((SomeClass)null)
Foo(null as SomeClass)
Foo(default(SomeClass))
Run Code Online (Sandbox Code Playgroud)
基本上,哪个是最好的选择?不同方法之间是否存在显着的性能差异?特定的方式通常被认为比其他方式更"优雅"吗?
谢谢
我已经定义了这样的功能:
template<typename T>
void SwapMe(T *first, T *second)
{
T tmp = *first;
*first = *second;
*second = tmp;
}
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
SwapMe(&data[i], &data[j]);
Run Code Online (Sandbox Code Playgroud)
如你所见,我没有明确使用,SwapMe<T>(...);但确实有效!
为什么C++标准允许避免显式指定参数的类型?
以下代码完美正常(显示RVO):
struct A {
A (int) { cout << "A::A()\n"; } // constructor
A (const A&) { cout << "A::A(const A&)\n"; } // copy constructor
};
A foo () { return A(0); }
int main () {
A a = foo();
}
Run Code Online (Sandbox Code Playgroud)
输出:
A::A() // --> which means copy constructor is not called
Run Code Online (Sandbox Code Playgroud)
如果我将复制构造函数标记为explicit:
explicit A (const A&) { ... }
Run Code Online (Sandbox Code Playgroud)
然后编译器出错了:
explicit.cpp: In function ‘A foo()’:
explicit.cpp:10:22: error: no matching function for call to ‘A::A(A)’
A foo …Run Code Online (Sandbox Code Playgroud) c++ compiler-errors explicit copy-constructor return-value-optimization
我operator bool()在课外定义函数有问题
class A{
public:
explicit operator bool() const;
};
Run Code Online (Sandbox Code Playgroud)
我在课外定义函数为......
explicit A::operator bool() const {
...
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误 - error: ‘explicit’ outside class declaration
做错了什么?
我有这个代码片段:
public interface Imy
{
int X { get; set; }
}
public class MyImpl : Imy
{
private int _x;
int Imy.X
{
get => _x;
set => _x = value;
}
}
class Program
{
static void Main(string[] args)
{
var o = new MyImpl();
o.Imy.X = 3;//error
o.X = 3;//error
}
}
Run Code Online (Sandbox Code Playgroud)
我只是想为 X 赋值,但得到 2 个编译错误。如何解决?
使用强制转换构造函数不好吗?否则为什么代码质量检查器(在我的例子中是 cppcheck)会不断建议在单参数构造函数之前添加显式?
如果我想做怎么办
class MyClass {
A(int) {}
};
A a = 1;
Run Code Online (Sandbox Code Playgroud)
如果我按照“建议”写
class MyClass {
explicit A(int) {}
};
A a = 1;
Run Code Online (Sandbox Code Playgroud)
会抛出一个错误,但如果我使用第一个,我会收到一个警告,我必须记录以通过代码审查。
explicit ×10
c++ ×6
c# ×4
templates ×2
.net ×1
arguments ×1
c#-4.0 ×1
c++11 ×1
casting ×1
compilation ×1
constructor ×1
cppcheck ×1
default ×1
function ×1
implicit ×1
inheritance ×1
interface ×1
overloading ×1
properties ×1