例如:
public interface IFoo
{
//...
ICollection<IFoo> Children { get; }
//...
}
public class Foo : IFoo
{
//...
public ICollection<Foo> Children { get; private set; }
//...
public ICollection<IFoo> IFoo.Children
{
get
{
return Children; // Obviously wrong datatype, how do I cast this?
}
}
//...
}
Run Code Online (Sandbox Code Playgroud)
那么我该如何正确实施IFoo.Children呢?我尝试了,Children.Cast<IFoo>但这会返回一个IEnumerable<IFoo>.
例如,以下是有效的C代码吗?
typedef struct {
/* ... */
} foo;
typedef struct {
foo foo; /* Is it ok that 'foo' == 'foo'? */
} bar;
Run Code Online (Sandbox Code Playgroud) 在Go中,字符串在内部存储为C-struct:
struct String // This is C code (not Go)
{
byte* str;
int32 len;
};
Run Code Online (Sandbox Code Playgroud)
假设我有以下变量:
a0 := "ap" // This is Go code
a1 := "ple"
b0 := "app"
b1 := "le"
a := a0 + a1
b := b0 + b1
c := "apple"
d := c
Run Code Online (Sandbox Code Playgroud)
然后是以下代码:
fmt.Println("a == b = %t, &a == &b = %t", a == b, &a == &b)
fmt.Println("c == d = %t, &c == &d = %t", c == d, …Run Code Online (Sandbox Code Playgroud) 在以下代码中:
i = x % b;
Run Code Online (Sandbox Code Playgroud)
当b函数为2时,模运算是否更快,即使b在编译时不知道(即使编译器不会将其优化为按位并且因为它不知道b它将是2的幂)?如果不是,如果我知道b永远是2的幂,我怎么能强制进行这样的优化呢?
编辑:我想在问题的第一部分我真正问的是divl指令(或类似的)本身是否会以2的幂运行得更快.
看起来像这样(示例显示教堂数字和Y组合器):
zero := ?.?.0
one := ?.0 -- or more verbosely: ?.?.1 0
two := ?.?.1 (1 0)
three:= ?.?.1 (1 (1 0))
add := ?.?.?.?.3 1 (2 1 0)
Y := ?.(?.1 (0 0)) (?.1 (0 0))
Run Code Online (Sandbox Code Playgroud)
这种符号的名称是什么?我似乎已经忘记了.
theory lambda functional-programming function lambda-calculus
例如:
type Foo struct {
x int
}
var foo *Foo = &Foo{5}
type Bar *struct {
x int
}
var bar Bar = ??
Run Code Online (Sandbox Code Playgroud)
我该如何初始化bar?
我意识到有一个解决方法:
type Bar *Foo
var bar Bar = &Foo{5}
Run Code Online (Sandbox Code Playgroud)
但我想避免这种情况.
例如,我试图做这样的事情:
class Animal {
public:
virtual const char *says() const = 0;
static Animal *makeLion() { return new Lion(); }
static Animal *makeTiger() { return new Tiger(); }
static Animal *makePig() { return new Pig(); }
private:
class Lion : public Animal { // error: invalid use of incomplete type ‘class Animal’
public:
virtual const char *says() const
{
return "roar";
}
};
class Tiger : public Animal { // error: invalid use of incomplete type ‘class Animal’
public:
virtual …Run Code Online (Sandbox Code Playgroud) 例如:
class Foo : public Bar
{
~Foo()
{
// Do some complicated stuff
}
Foo &operator=(const Foo &rhs)
{
if (&rhs != this)
{
~Foo(); // Is this safe?
// Do more stuff
}
}
}
Run Code Online (Sandbox Code Playgroud)
在继承和其他类似的事情上明确调用析构函数会产生什么意外后果吗?
有没有理由将析构函数代码抽象为void destruct()函数并调用它?