如何实例化接口的实现
How to instantiate an implementation of interface?
我有两个属性:
UPROPERTY()
TScriptInterface<ICoordinateSystem> CoordinateSystem;
UPROPERTY(EditAnywhere)
TSubclassOf<UCoordinateSystem> CoordinateSystemType;
如何实例化坐标系统类型并将结果存储到坐标系统?我尝试了不同的方式,例如
CoordinateSystem = NewObject<ICoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType);
CoordinateSystem = NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType);
CoordinateSystem = Cast<ICoordinateSystem>(NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType));
但是他们都没有编译说它不能在NewObject或TScriptInterface =运算符中将UObject*转换为ICoordinateSystem*或ICoordinateSystem*到UObject*。我在UE 4.11和4.12中尝试过
编辑:对于代码
CoordinateSystem = Cast<ICoordinateSystem>(NewObject<UCoordinateSystem>((UObject *) GetTransientPackage(), *CoordinateSystemType));
我收到错误:
1>------ Build started: Project: UE44X, Configuration: DebugGame_Game x64 ------
1> Creating makefile for UE44X (working set of source files changed)
1> Parsing headers for UE44X
1> Running UnrealHeaderTool "F:UE4UE44X 4.12UE44X.uproject" "F:UE4UE44X 4.12IntermediateBuildWin64UE44XDebugGameUE44X.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
1> Reflection code generated for UE44X in 4,8843075 seconds
1> Performing 2 actions (4 in parallel)
1> UE44XGameMode.cpp
1>C:Program Files (x86)Epic Games4.12EngineSourceRuntimeCoreUObjectPublicUObjectScriptInterface.h(150): error C2664: 'void FScriptInterface::SetObject(UObject *)': cannot convert argument 1 from 'ICoordinateSystem *' to 'UObject *'
1> C:Program Files (x86)Epic Games4.12EngineSourceRuntimeCoreUObjectPublicUObjectScriptInterface.h(150): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> F:UE4UE44X 4.12SourceUE44XUE44XGameMode.cpp(26): note: see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<To>(UObjectType *)' being compiled
1> with
1> [
1> InterfaceType=ICoordinateSystem,
1> To=ICoordinateSystem,
1> UObjectType=ICoordinateSystem
1> ]
1> F:UE4UE44X 4.12SourceUE44XUE44XGameMode.cpp(26): note: see reference to function template instantiation 'InterfaceType &TScriptInterface<InterfaceType>::operator =<To>(UObjectType *)' being compiled
1> with
1> [
1> InterfaceType=ICoordinateSystem,
1> To=ICoordinateSystem,
1> UObjectType=ICoordinateSystem
1> ]
1>ERROR : UBT error : Failed to produce item: F:UE4UE44X 4.12BinariesWin64UE44X-Win64-DebugGame.pdb
1> Total build time: 11,90 seconds
1>C:Program Files (x86)MSBuildMicrosoft.Cppv4.0V140Microsoft.MakeFile.Targets(37,5): error MSB3075: The command ""C:Program Files (x86)Epic Games4.12EngineBuildBatchFilesBuild.bat" UE44X Win64 DebugGame "F:UE4UE44X 4.12UE44X.uproject" -waitmutex" exited with code 5. Please verify that you have sufficient rights to run this command.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
接口通常被视为声明函数的类,但这些函数通常在子类中实现。
在UE4中,界面不是UObject。 您需要创建一个派生自接口的类。
class MyClass : public UObject, public ICoordinateSystem
{
//Overrides functions from ICoordinateSystem
....
};
您将能够实例化 MyClass。 要获取 MyClass 坐标系统的句柄,请使用 Cast 模板...
MyClass* myClassInstance;
....
ICoordinateSystem* coordinateInterface = Cast<ICoordinateSystem>(myClassInstance);
希望本文将有助于阐明如何使用接口:https://wiki.unrealengine.com/Interfaces_in_C%2B%2B
相关文章:
- 如何从实现接口的模板化类实例访问结构
- 如何在C++中实现接口
- unique_ptr实现接口时对已删除函数的引用
- 避免对实现接口的类使用多个几乎相同的声明
- C#COM组装实现C 接口
- 当传递对类的引用时,优于另一个函数,即实现接口
- 通过其方法的子集实现接口
- C ++类实现接口,接口具有采用任何实现该接口的类的方法
- 错误:在 C++ 中实现接口模式时未定义对 typeinfo 的引用
- 与模板实现接口的类模板
- 在主应用程序中声明的 dll 中实现接口 - C++
- Objective-C类实现C 接口
- 如何添加实现C 接口的标签
- 强制接收实现接口的超类指针作为方法的参数
- 我可以部分实现接口层次结构吗
- 是否可以通过继承实现接口
- 在c++中实现接口
- 如何使用谷歌测试测试实现接口的不同类/结构
- 使用成员函数模板实现接口的功能
- C++ 11 实现接口的方法不可用。为什么?