C++/CLI and C# singleton

本文关键字:singleton and CLI C++ | 更新日期: 2023-09-27 18:11:04

我有一个cpp/cli dll有一个DataMiner类。它是一个单元素。我所有的cpp/cli代码都在DataMine命名空间中。

public ref class DataMiner
{
private:
    DataMiner();
    void(*stdout_callback) (const char* cb);
    static DataMiner dm_instance;
public:
    static property DataMiner^ Instance { DataMiner^ get() { return %dm_instance; } }
    void setCB(System::IntPtr cb);
    void status_print(std::string s);
    void K_Means(array<float>^ data);
};

我也有一个cpp/cli类,我用它来做一些数学:

public ref class KMeans
{
private:
    int _N;
    int _K;
    int* _kMembership;
    float _kSSE;
    float* _sse;
    float* _individualSE;
    float* _data;
    float* _centroids;
    float** _distances;
    KMeans() {};
    //void reformClusters();
public:
    KMeans(float* data, int N, int K);
    KMeans(float* data, float* centroids, int N, int K);
    ~KMeans();
};
在c#端,我有以下(正常工作)行:
DataMiner dm = DataMiner.Instance;
dm.setCB(Marshal.GetFunctionPointerForDelegate(MainWindow.Instance.statusWindow.dz));

但是,当我在cpp/cli端KMeans构造函数中尝试相同的行时,它不起作用:

KMeans(float* data, int N, int K)
{
    // (some code)
    DataMiner dm = DataMiner.Instance;
    // (some more code)
}

我得到的实际错误消息是:

DataMine::DataMiner : illegal use of this type as an expression left of '.Instance' must have class/struct/union
'DataMine::DataMiner::DataMiner' : candidate function(s) not accessible

C++/CLI and C# singleton

我的c++/CLI有点生疏,但我认为你想使用

DataMiner dm = DataMiner::Instance;

因为::运算符用于静态成员