小编Jef*_*nes的帖子

ICollection <T>不协变?

这样做的目的是同步包含图形边缘的两个集合,发送方和接收方,以便在发生某些事情时(删除边缘,添加边缘等)通知双方.

为此,对集合的(反向)引用包含在集合中的元素中

class EdgeBase {
    EdgeBase(ICollection<EdgeBase> rCol, ICollection<EdgeBase> sCol)     
    { RecvCol=rCol;  SendCol=sCol; }      
    ICollection<EdgeBase> RecvCol;      
    ICollection<EdgeBase> SendCol;       
    public virtual void Disconnect() // Synchronized deletion         
    { RecvCol.Remove(this);  SendCol.Remove(this); }                 
}         
class Edge : EdgeBase {       
    Edge(ICollection<EdgeBase> rCol, ICollection<EdgeBase> sCol)     
    : base(rCol, sCol) {}
    int Weight;     
}      
Run Code Online (Sandbox Code Playgroud)

删除(断开连接)没问题,但在创建过程中出现了问题:

HashSet<Edge> receiverSet, senderSet;
var edge = new Edge(receiverSet, senderSet); // Can't convert Edge to EdgeBase!
Run Code Online (Sandbox Code Playgroud)

虽然Edge是源于EdgeBase此,但这是非法的.(问题是Edge一部分,而不是HashSet<>一部分.)

在写完数百行之后,我发现ICollection<>它并不是协变的IEnumerable<>.

什么是解决方法?

编辑:

如果我在不破坏C#的协方差规则的情况下编写上面的代码,那就像这样:

public class …
Run Code Online (Sandbox Code Playgroud)

c# generics inheritance covariance

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

获取指向迭代器引用的STL容器的指针?

例如,以下是可能的:

std::set<int> s;  
std::set<int>::iterator it = s.begin();
Run Code Online (Sandbox Code Playgroud)

我想知道是否可能相反,比方说,

std::set<int>* pSet = it->**getContainer**();  // something like this...
Run Code Online (Sandbox Code Playgroud)

c++ stl

10
推荐指数
3
解决办法
3648
查看次数

_WIN64 未在 x64 项目中定义

我正在使用 VS2008 并且项目属性设置为(活动)x64

奇怪的是 _WIN64 没有定义,WPARAM 和 LPARAM 仍然是 32 位。

我应该手动定义 _WIN64 吗?

如果是这样,我应该把#define _WIN64 放在哪里?预处理器设置似乎不起作用。

我很确定它被编译为 x64,因为在任务管理器中,exe 没有 *32 后缀。

c++ api 64-bit winapi

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

memberwiseclone还会复制派生类中定义的成员吗?

我需要定义一个执行浅拷贝的Clone()方法.(不需要深层复制)

但我需要复制派生类的成员.

如果我有

class Base {
     int baseMember;
     public (virtual?) Base Clone() {
         return (Base)this.MemberwiseClone()
     }
}
Run Code Online (Sandbox Code Playgroud)

那么我应该为Clone()派生所有其他类吗?derivedMember也会被Base.Clone()复制吗?

class Derived {
    int derivedMember;  //will this also be copied by base.Clone()?

    //Necessary?
    public new Derived (override Base?) Clone() {
        return (Derived)this.MemberwiseClone();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# clone copy

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

获取当前数据库中定义的所有关系类型

有没有有效的方法来获取当前定义的所有关系类型?

我知道这有效:

match ()-[r]-() RETURN DISTINCT TYPE(r)
Run Code Online (Sandbox Code Playgroud)

但我想,如果关系数量很大并且底层没有固有的索引,这将消耗大量时间。

neo4j neo4j-browser

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

传递shared_array <T>参数

下面的代码在取消注释时崩溃,似乎get()中的shared_array <>参数有问题.

print()似乎至少暂时没有崩溃......

传递shared_array <>参数的正确方法是什么?

#include <iostream>
#include <cstring>
#include <boost/shared_array.hpp>
using namespace std;
using namespace boost;

shared_array<wchar_t> get(const wchar_t* s) {
//shared_array<wchar_t> get(const shared_array<wchar_t>& s) {

    size_t size = wcslen(s);
    //size_t size = wcslen(s.get());

    shared_array<wchar_t> text(new wchar_t[size+1]);

    wcsncpy(text.get(), s, size+1);
    //wcsncpy(text.get(), s.get(), size+1);

    return text;
}

void print(shared_array<wchar_t> text) {
    wcout << text.get() << endl;
}

int wmain(int argc, wchar_t *argv[]) {
    //shared_array<wchar_t> param(argv[1]);

    shared_array<wchar_t> text = get(argv[1]);
    //shared_array<wchar_t> text = get(param);

    print(text);
    //print(text.get()); 
}
Run Code Online (Sandbox Code Playgroud)

编辑:谢谢.所以关键在于我在使用boost :: shared_ptr/array时应该总是只使用new/new []. …

c++ boost pointers shared-ptr

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

标签 统计

c++ ×3

c# ×2

64-bit ×1

api ×1

boost ×1

clone ×1

copy ×1

covariance ×1

generics ×1

inheritance ×1

neo4j ×1

neo4j-browser ×1

pointers ×1

shared-ptr ×1

stl ×1

winapi ×1