小编Mat*_*att的帖子

在C#中,将ICollection <X>转换为ICollection <Y>的正确方法是什么?

例如:

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>.

.net c# interface

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

在C中,结构成员可以使用与类型相同的名称吗?

例如,以下是有效的C代码吗?

typedef struct {
    /* ... */
} foo;

typedef struct {
    foo foo; /* Is it ok that 'foo' == 'foo'? */
} bar;
Run Code Online (Sandbox Code Playgroud)

c syntax struct

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

有没有办法判断两个字符串是否在Go中共享内存?

在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)

string equality go

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

即使在编译时不知道该值,模幂运算是否更快,功率为2?

在以下代码中:

i = x % b;
Run Code Online (Sandbox Code Playgroud)

b函数为2时,模运算是否更快,即使b在编译时不知道(即使编译器不会将其优化为按位并且因为它不知道b它将是2的幂)?如果不是,如果我知道b永远是2的幂,我怎么能强制进行这样的优化呢?

编辑:我想在问题的第一部分我真正问的是divl指令(或类似的)本身是否会以2的幂运行得更快.

c c++ assembly modulo

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

使用整数偏移来引用隐式单个参数的lambda表示法的名称是什么?

看起来像这样(示例显示教堂数字和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

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

如何在Go中初始化一个指向结构的指针的类型?

例如:

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)

但我想避免这种情况.

struct types pointers initialization go

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

为什么多态派生类不能嵌套在基类中?

例如,我试图做这样的事情:

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)

c++ polymorphism inheritance inner-classes

1
推荐指数
2
解决办法
288
查看次数

从赋值运算符调用析构函数会产生什么意外后果吗?

例如:

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()函数并调用它?

c++ destructor assignment-operator

0
推荐指数
1
解决办法
77
查看次数