简体   繁体   中英

How to make a static class to reference string variables with C++?

So I am trying to make a general C++ static that just holds string variables. I am using Unreal Engine 4 in this project. I have a working solution, but am looking to see if what I do in C# can be done in C++.

Working Solution (C++): DFControllerGameModeBase.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "DFControllerGameModeBase.generated.h"


#define MoveForwardInput        "MoveForward"

Implementation:

#include "DFControllerGameModeBase.h" 

void ADFCharacter::Play(){
.....

string text = MoveForwardInput;

}

However this is what I do in C# with Unity:

using System;

namespace Assets.Scripts.Helpers
{

    public static class Utilities
    {
        public static string MoveForward = "MoveForward";
    }
}

Implementation:

using Assets.Scripts.Helpers;

void Play(){

string text = Utilities.MoveForward;

}

If you have no problem with encapsulation and will not be using the statics in Blueprint, then just using #define will do.

You also want to use capital letters for #define naming convention.

#define MOVE_FORWARD_INPUT "MoveForward"

But if you want it encapsulated and be able to call it in the Blueprint. Then you'll have to create a static helper class.

.h
class Utilities
{
public:
    static const FString MoveForward;

    //create BP getter function here
}

.cpp
const FString Utilities::MoveForward = "MoveForward";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM