检查下面的代码:
class Money
{
public Money(decimal amount)
{
Amount = amount;
}
public decimal Amount { get; set; }
public static implicit operator decimal(Money money)
{
return money.Amount;
}
public static explicit operator int(Money money)
{
return (int)money.Amount;
}
}
Run Code Online (Sandbox Code Playgroud)
我不明白它在我的代码中会有用,我不能只做一个像这样的方法:
public static int returnIntValueFrom(Money money)
{
return (int)money.Amount;
}
Run Code Online (Sandbox Code Playgroud)
实施不是更容易和更清楚吗?
在C#我可以表达这个:
var xe = XElement.Parse("<foo></foo>");
var maybe = (bool?)xe.Element("bar");
Run Code Online (Sandbox Code Playgroud)
如何在F#中表达?
编辑:我确实找到了这个辅助函数
let inline conv (x : ^a) : ^b = ((^a or ^b) : (static member op_Explicit : ^a -> ^b) (x))
Run Code Online (Sandbox Code Playgroud) xelement f# linq-to-xml operator-keyword explicit-conversion
我试图找出防止整数 0 隐式转换为 nullptr_t 然后传递给采用指针的构造函数的最佳方法。显式不会这样做,但我可以获得 nullptr_t 来导致不明确的重载错误:
#include <typeinfo.h>
struct A {
explicit A(char*) { }
};
struct B {
B(nullptr_t a) = delete;
B(char*) { }
};
int main(int argc, char* argv[])
{
A a(0); // darnit I compiled...
B b1(0); // good, fails, but with only b/c ambiguous
B b2((char*)0); // good, succeeds
B b3(1); // good, fails, with correct error
}
Run Code Online (Sandbox Code Playgroud)
还有比这更好的方法吗?另外,删除在这里到底要完成什么?
c++ implicit-conversion explicit-conversion c++11 visual-studio-2013
这是允许的吗?如果没有,这可以通过重载隐式/显式转换运算符来固有地完成吗?
c# casting operator-overloading implicit-conversion explicit-conversion
当发现以下代码在运行时抛出异常时,我感到很惊讶:
class A
{
public string Name { get; set; }
public A()
{
Name = "Class A";
}
}
class B
{
public string Name { get; set; }
public B()
{
Name = "Class B";
}
public static explicit operator A(B source)
{
return new A() {Name = source.Name};
}
}
class Program
{
static void Main(string[] args)
{
// This executes with no error
var bInstance = new B();
Console.WriteLine(bInstance.GetType()); // <assemblyname>.B
var aInstance = (A) bInstance; …Run Code Online (Sandbox Code Playgroud) 为什么不能在同一个类中共存两个相同类型的运算符(显式和隐式)?假设我有以下内容:
public class Fahrenheit
{
public float Degrees { get; set; }
public Fahrenheit(float degrees)
{
Degrees = degrees;
}
public static explicit operator Celsius(Fahrenheit f)
{
return new Celsius(ToCelsius(f.Degrees));
}
public static implicit operator Celsius(Fahrenheit f)
{
return new Celsius(ToCelsius(f.Degrees));
}
}
public class Celsius {
public float Degrees { get; set; }
public Celsius(float degrees)
{
Degrees = degrees;
}
}
Run Code Online (Sandbox Code Playgroud)
所以我可以给客户端使用两种方式中的任何一种,例如:
Fahrenheit f = new Fahrenheit(20);
Celsius c1 = (Celsius)f;
Celsius c2 = f;
Run Code Online (Sandbox Code Playgroud)
是否有任何特殊原因不允许这样做,或者只是避免滥用程序员的惯例?
c# operator-overloading implicit-conversion explicit-conversion
我有一行代码
double i = 1 + (long)1.5* 5.0f
Run Code Online (Sandbox Code Playgroud)
我的问题是转换顺序和结果是什么?一直在寻找这样的例子,但无济于事.有什么好的指南可以帮助我理解它吗?
以下示例包含两个模板化类,用于表示度和弧度,并使用显式转换运算符在它们之间进行转换.它使用g ++(ideone链接)编译和运行,但不使用Visual Studio 2013 Visual C++ Compiler Nov 2013 CTP (CTP_Nov2013)作为平台工具集.
#include <iostream>
static const double PI = 3.14159265358979323846;
// Forward declarations
template< typename T > class radians;
template< typename T > class degrees;
template< typename T >
class degrees
{
public:
degrees(const T value)
: value_(value)
{}
template< typename U >
explicit operator U() const
{
return value_ * PI / 180.0;
}
T value() const { return value_; }
private:
T value_;
};
template< typename …Run Code Online (Sandbox Code Playgroud) 您可以隐式地将int转换为double: double x = 5;
您可以显式地将int转换为double: double x = (double) 5;
您可以显式地将double转换为int: int x = (int) 5.0;
为什么不能隐式地将double转换为int?: int x = 5.0;
我正在使用一个结构,需要隐式运算符对字符串,并遇到一个我没有想过的基本问题.
public static implicit operator Version (string value) {...}
Run Code Online (Sandbox Code Playgroud)
我可以理解只有显式运算符来强制执行转换,但如果隐式运算符已经过载,则无法想到需要它的情况.有吗?
.net c# operator-overloading implicit-conversion explicit-conversion