-
UCLASS(abstract)
-
class ENGINE_API AActor : public UObject
-
{
-
...
-
}
Hi, I’m wondering why if writing pure virtual methods in my abstract base classes is supported? Currently I’ve got a class that inherits from actor and I’ve marked it as UCLASS(abstract) with a pure virtual method.
I then have a class that inherits from this class, and implements the pure virtual method. When I compile this I get the following error.
Only functions in the second interface class can be declared abstract.
Does that mean if I want to use pure virtual methods in my abstract class that I have to use a separate interface? If that’s the case, then what is the purpose of declaring my class abstract? Aren’t pure virtual methods what make classes abstract?
UCLASS(abstract)
class ASpawnVolume : public AActor
{
GENERATED_UCLASS_BODY()
public:
UFUNCTION(BlueprintCallable, Category = "Spawning")
virtual void SpawnUnitWithTransform(TSubclassOf<AActor> unitType, FTransform trans);
UFUNCTION(BlueprintCallable, Category = "Spawning")
virtual void SpawnUnit(TSubclassOf<AActor> unitType);
UFUNCTION(BlueprintCallable, Category = "Spawning")
virtual void SpawnUnits(TSubclassOf<AActor> unitType, uint32 count);
UFUNCTION(BlueprintCallable, Category = "Spawning")
virtual FTransform GetRandomTransformInVolume() = 0;
};
UClasses cannot really be abstract in the C++ sense, because the UObject sub-system requires that each class can be instantiated (it creates at least one instance of each class as a so called Class Default Object [CDO] that holds the default properties of that class). Therefore, every class method must have an implementation, even if it does nothing.
That being said, you can get similar behavior by decorating your inline method implementations with the PURE_VIRTUAL macro. This will tell the UObject sub-system that your intent is to declare a pure virtual method. So even though the method is not pure virtual in the C++ sense - it has a (possibly empty) function body - the compiler can still ensure that all child classes do supply an actual implementation.
For example, you could do:
virtual void SpawnUnit(TSubclassOf<AActor> unitType) PURE_VIRTUAL(ASpawnVolume::CanRedo,);
You can find many more examples in the code base, including methods with return values by searching for PURE_VIRTUAL.
I’d like to add to this that the definition of the PURE_VIRTUAL macro is defined as
#define PURE_VIRTUAL(func,extra) { LowLevelFatalError(TEXT("Pure virtual not implemented (%s)"), TEXT(#func)); extra }.
In case you want a pure virtual with return type, the return must be part of the PURE_VIRTUAL macro. as it must be a complete function in the CPP sense.
virtual bool IsValid() PURE_VIRTUAL(MyClass::IsValid, return false;);
ref : http://egloos.zum.com/sweeper/v/3205742
ref : https://forums.unrealengine.com/t/how-do-i-implement-pure-virtual-methods/280323
'게임엔진(GameEngine) > Unreal4' 카테고리의 다른 글
UE4 RPC - Replicate (0) | 2022.09.15 |
---|---|
배열 타입 Json 읽어들이기 [Json Array To Unreal Engine [C++] ] (0) | 2022.08.31 |
This Repository shows a Code Example of how to use UE4 Online Subsystem Sessions in C++. (0) | 2022.08.04 |
What does Get Player Controller (Player index = 0) return in multiplayer? (0) | 2022.07.27 |
UE4 Get IP Adress C++ (0) | 2022.07.22 |