gpt4 book ai didi

指针成员的 c++/CLI 包装器 - c++ 单例实例

转载 作者:行者123 更新时间:2023-11-30 05:48:57 27 4
gpt4 key购买 nike

我正在为以下 native C++ 类完成我的 C++/CLI 包装器:

#ifndef __TV3DENGINE_H__
#define __TV3DENGINE_H__
#pragma once

#include "TV3DMoteur.h"
#include "Input.h"
#include "Area.h"
#include <vcclr.h>

class Engine3D
{
public:
CLTV3DMoteur* clTV3D;
CLInput* clInput;
CLArea* clArea;
CLGlobalVar * clGlobalVar;

Engine3D();
~Engine3D();

void Setup(HWND TVScreenHWND, string PathString);
void UpdateLoop();
void Cleanup();
bool AppStillIdle();

CLTV3DMoteur* GetTV3D();
CLInput* GetInput();
CLArea* GetArea();
CLGlobalVar * GetGlobalVar();
};
#endif

Engine3D 的实际构造函数是:

Engine3D::Engine3D()
{
clTV3D = CLTV3DMoteur::getInstance();
clInput = CLInput::getInstance();
clArea = CLArea::getInstance();
clGlobalVar = CLGlobalVar::getInstance();
}

这是实际的包装器:

#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclr\marshal_cppstd.h>


public ref class Engine3DWrapper {
Engine3D* m_nativeClass;

public:
Engine3DWrapper() { m_nativeClass = new Engine3D(); }
~Engine3DWrapper() { delete m_nativeClass; }
void Setup(System::IntPtr tvscreen, System::String^ AppPath) {
System::String^ managedPath = AppPath;
m_nativeClass->Setup((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(managedPath));
}
void UpdateLoop() {
m_nativeClass->UpdateLoop();
}
void Cleanup() {
m_nativeClass->Cleanup();
}
bool AppStillIdle() {
return(m_nativeClass->AppStillIdle());
}

protected:
!Engine3DWrapper() { delete m_nativeClass; }
};
#endif

我的问题是如何修改我的 Wrapper,以便我可以访问 Engine3DWrapper->clGlobalVar->BLABLABLA(),其中 BLABLABLA 是 CLGlobalVar c++ 单例中定义的所有不同方法?

我尝试过这种技术:

 property String ^Name
{
String ^get()
{
return gcnew String(_stu->getName());
}
}

但这似乎不可能,因为我不需要返回定义的类型。

感谢您的帮助。

最佳答案

问题解决了。这是根据 Rufflewind 建议更正后的 Wrapper:

#ifndef __WRAPPER_H__
#define __WRAPPER_H__
#pragma once
#include "TV3DEngine.h"
#include <msclr\marshal_cppstd.h>


public ref class Engine3DWrapper {
Engine3D* m_nativeClass;

public:
Engine3DWrapper(System::IntPtr tvscreen, System::String^ AppPath)
{
m_nativeClass = new Engine3D((HWND)tvscreen.ToInt32(), msclr::interop::marshal_as<std::string>(AppPath));
m_TV3D = m_nativeClass->GetTV3D();
m_Input = m_nativeClass->GetInput();
m_Area = m_nativeClass->GetArea();
m_GlobalVar = m_nativeClass->GetGlobalVar();
}
~Engine3DWrapper() {
delete m_nativeClass;
}
void UpdateLoop() {
m_nativeClass->UpdateLoop();
}
void Cleanup() {
m_nativeClass->Cleanup();
}
bool AppStillIdle() {
return(m_nativeClass->AppStillIdle());
}

CLTV3DMoteur* m_TV3D;
CLInput* m_Input;
CLArea* m_Area;
CLGlobalVar* m_GlobalVar;

protected:
!Engine3DWrapper() { delete m_nativeClass; }
};
#endif

在 native 类中使用简单的 Get 方法:

CLGlobalVar *Engine3D::GetGlobalVar()
{
clGlobalVar = CLGlobalVar::getInstance();
return(clGlobalVar);
}

谢谢你的帮助!

关于指针成员的 c++/CLI 包装器 - c++ 单例实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27973668/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com