在此代码中,分配给b1有效,但它不允许分配给b2(有或没有静态强制转换).我实际上是试图解决相反的问题,公共继承但不是隐式转换为基础.然而似乎从未使用过演员.为什么是这样?
struct B {};
struct D1 : private B {
operator B&() {return *this;}
B& getB() {return *this;}
};
struct D2 : public B {
explicit operator B&() {return *this;}
};
struct D3 : public B {
operator B&() = delete;
};
void funB(B& b){}
int main () {
D1 d1;
funB(d1.getB()); // works
// funB(d1); // fails to compile with 'inaccessible base class
D2 d2;
funB(d2); // works
D3 d3;
funB(d3); // works
return 0;
}
Run Code Online (Sandbox Code Playgroud) class B {};
class D : protected B {};
D d;
B *p = &d; // conversion from 'D *' to 'B &' exists, but is inaccessible
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中遇到此错误,最后我通过进行显式转换设法编译它:
D d;
B *p = (B*)&d;
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么通过使D类继承受B保护而使得隐式转换不可访问.
我试图通过在D类中创建一个运算符B()来避免显式转换,以使转换可访问:
class B {};
class D : protected B
{
public:
operator B() {return *this;}
};
Run Code Online (Sandbox Code Playgroud)
但是没有办法.
任何其他避免显式转换的解决方案?
当x是来自用户定义的类的对象时,在Console.WriteLine(x)中完成的隐式转换的顺序是什么:
class Vector
{
public int x = 12;
public static implicit operator double(Vector v1)
{
return 3.14;
}
public static implicit operator int(Vector v1)
{
return 42;
}
public override string ToString()
{
return this.x.ToString();
}
}
static void Main(string[] args)
{
Vector v11 = new Vector();
Console.WriteLine(v11);
}
Run Code Online (Sandbox Code Playgroud)
为什么我得到42,而不是3.14或"12"?为什么我不能向字符串添加额外的隐式转换/ CW(int)和CW(字符串)/之间存在歧义的编译器错误:
public static implicit operator string(Vector v1)
{
return "42";
}
Run Code Online (Sandbox Code Playgroud)
我知道我应该避免使用隐式演员,但仅仅是为了好奇!
由于复合赋值和递增/递减运算符中的隐式转换,以下编译:
byte b = 0;
++b; b++; --b; b--;
b += b -= b *= b /= b %= b;
b <<= b >>= b >>>= b;
b |= b &= b ^= b;
Run Code Online (Sandbox Code Playgroud)
并且由于自动装箱和自动拆箱,以下还编译:
Integer ii = 0;
++ii; ii++; --ii; ii--;
ii += ii -= ii *= ii /= ii %= ii;
ii <<= ii >>= ii >>>= ii;
ii |= ii &= ii ^= ii;
Run Code Online (Sandbox Code Playgroud)
然而,以下代码段中的最后一行给出了编译时错误:
Byte bb = 0;
++bb; bb++; --bb; bb--; // ... okay …Run Code Online (Sandbox Code Playgroud) java autoboxing compiler-errors implicit-cast compound-assignment
我能够找到大量关于从int到用户定义类型的隐式转换的信息.即如果构造函数将int作为其参数并且不以"explicit"开头,则可能发生隐式转换.
如果我希望我的类隐式转换为 int怎么办?
例如,需要在SimpleClass内部或外部添加什么函数,以便main函数将编译并输出"1"到控制台?(看评论)
#include <iostream>
class SimpleClass
{
private:
int m_int;
public:
SimpleClass(int value)
: m_int(value) {}
};
int main(int argc, const char * argv[])
{
SimpleClass simpleclass(1);
int i = simpleclass; // does not complile
std::cout << i << std::endl; // should output "1" to the console
return 0;
}
Run Code Online (Sandbox Code Playgroud) 为什么以下在TypeScript中编译?
enum xEnum {
X1,X2
}
function test(x: xEnum) {
}
test(6);
Run Code Online (Sandbox Code Playgroud)
它不应该抛出错误吗?恕我直言这隐式演员在这里是错的,不是吗?
这是游乐场的链接.
short BitwiseTest(short value)
{
short test1 = ((value >> 8) & 0xFF);
short test2 = unchecked((short)((value << 8) & 0xFF00));
return (test1 | test2);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码应该是一个(低效)示例,它在C#中交换短(带符号的16位整数)的字节序.
但是上面的代码不会编译,因为C#在以下两行中隐式地从short转换为int:
第一种情况:
short test1 = ((value >> 8) & 0xFF);
Run Code Online (Sandbox Code Playgroud)
第二种情况:
return (test1 | test2);
Run Code Online (Sandbox Code Playgroud)
为什么要进行演员表演?我是否可以通过简短的回归来实现预期的结果?像这样:
short BitwiseTest2(short value)
{
short test1 = (short)((value >> 8) & 0xFF);
short test2 = unchecked((short)((value << 8) & 0xFF00));
return ((short)(test1 | test2));
}
Run Code Online (Sandbox Code Playgroud)
如果不是为什么不呢?
请注意,我确实理解为什么C#在执行左位移时将短路转换为整数,因此分配了test2变量.
我正在处理代码,该代码将特定协议定义的大量ascii文本作为输入.原作者将原始协议中的"string(1)"数据类型解释为代码中的字符.
在角落的情况下有很多微妙的错误,你有代码,如:
char theChar = whatever();
if(theChar == 7) {...}
Run Code Online (Sandbox Code Playgroud)
真正的意思是:
if(theChar == '7') {...}
Run Code Online (Sandbox Code Playgroud)
为了尝试一次捕获所有这些,有没有办法禁用隐式转换为'char'?如果没有,那么追踪所有这些的最佳方法是什么?
Mapper Automap:
Mapper.CreateMap<ObjectType1, ObjectType2>()
.ForMember(o1 => o1.PropName, mapper => mapper.MapFrom(o2 => o2.Prop2Name));
Mapper.Map(object1, object2);
Run Code Online (Sandbox Code Playgroud)
隐含运算符:
public static implicit operator Object1(Object2 o2)
{
Object1 o1 = new Object2();
//Mapping code here...
return o1;
}
Run Code Online (Sandbox Code Playgroud) 编译器的某些功能让我感到困惑(使用Eclipse的Oracle JDK 1.7).
所以我有这本书说char原语需要明确地转换为short和byte,这一切都有意义,因为数据类型的允许范围不重叠.
换句话说,下面的代码可以工作(但如果没有显式类型转换,则无法工作):
char c = '&';
byte b = (byte)c;
short s = (short)c;
Run Code Online (Sandbox Code Playgroud)
正确打印b或s会显示数字38,这是Unicode中(&)的数字等效值.
这让我想到了我的实际问题.为什么以下工作也一样?
byte bc = '&';
short sc = '&';
System.out.println(bc); // Correctly displays number 38 on the console
System.out.println(sc); // Correctly displays number 38 on the console
Run Code Online (Sandbox Code Playgroud)
现在我肯定会理解以下内容(也适用):
byte bt = (byte)'&';
System.out.println(bt); // Correctly displays number 38 on the console
Run Code Online (Sandbox Code Playgroud)
但是对于字节(和短)"潜行转换"这个无编译器警告字符对我来说似乎不对.
有人可以解释,为什么允许这样做?
原因可能在于对'<char>'自身的解释,因此它实际上并没有达到char原始状态,而是作为数字(八进制或十六进制等)值处理?
implicit-cast ×10
c# ×4
c++ ×3
casting ×3
inheritance ×2
java ×2
autoboxing ×1
automapper ×1
byte ×1
char ×1
implicits ×1
syntax ×1
typescript ×1