如何在C++中编写类似Java的枚举

hwamh0ep  于 2023-01-15  发布在  Java
关注(0)|答案(1)|浏览(203)

我刚开始学习C++。我正在尝试将我编写的Java应用程序转换为C++。我不明白的是如何将Enumeration类从Java重新创建为C++。我希望能够创建具有构造函数和toString()方法的Enumeration对象。
下面是我的Java枚举的一个例子:

public enum InteractionType {
    // Enumeration Values
        PRINTACTIONS("Print Available Actions"),
        PRINTBANKNAME("Display Bank Name"),
        PRINTBANKACTIONS("Display Bank Actions"),
        CREATEBRANCH("Create a Branch"),
        CREATECUSTOMER("Create a Customer"),
        READBRANCH("Query Bank Branches"),
        READCUSTOMER("Query Customers and/or Customer Transactions"),
        UPDATEBRANCH("Update Branch Name"),
        UPDATECUSTOMER("Update Customer Information"),
        DELETEBRANCH("Delete Branch"),
        DELETECUSTOMER("Delete Customer"),
        QUITAPP("Quit Application");
        
    // Private Variables
    private final String description;

    // Constructor
    InteractionType(String description) {
        this.description = description;
    }

    // Methods
    public String toString() {
        return description;
    }
}

然后,我将在主文件中创建一个方法,以从值数组中获取特定的InteractionType

private static InteractionType getSelectedAction() {
    // Creates an array with all the values of the Enumeration Class
        InteractionType[] interactionTypes = InteractionType.values();
        System.out.print("Select an action: ");
        int id = scanner.nextInt();
        scanner.nextLine();
    // Returns a specific InteractionType
        return interactionTypes[id];
    }

这是我尝试编写的类,用于在C++中重新创建Java枚举:
InteractionType.h

#include <iostream>
#include <string>
using namespace std;

class InteractionType {
    public:
        static const InteractionType PRINTACTIONS(string = "Print Actions");
        static const InteractionType PRINTBANKNAME(string = "Print Bank Name");
        static const InteractionType PRINTBANKACTIONS(string = "Print Bank Actions");
        static const InteractionType CREATEBRANCH(string = "Create a Branch");
        static const InteractionType CREATECUSTOMER(string = "Create a Customer");
        static const InteractionType READBRANCH(string = "Read Branches");
        static const InteractionType READCUSTOMER(string = "Read Customers");
        static const InteractionType UPDATEBRANCH(string = "Update a Branch");
        static const InteractionType UPDATECUSTOMER(string = "Update a Customer");
        static const InteractionType DELETEBRANCH(string = "Delete a Branch");
        static const InteractionType DELETECUSTOMER(string = "Delete a Customer");
        static const InteractionType QUITAPP(string = "Quit Application");
    private:
        string description;
        InteractionType(string description);
};

InteractionType.cpp

#include "InteractionType.h"

const InteractionType InteractionType::PRINTACTIONS(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::PRINTBANKNAME(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::PRINTBANKACTIONS(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::CREATEBRANCH(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::CREATECUSTOMER(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::READBRANCH(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::READCUSTOMER(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::UPDATEBRANCH(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::UPDATECUSTOMER(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::DELETEBRANCH(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::DELETECUSTOMER(string description)
{
    return InteractionType(description);
}

const InteractionType InteractionType::QUITAPP(string description)
{
    return InteractionType(description);
}

InteractionType::InteractionType(string description) {
    this->description = description;
}
jc3wubiy

jc3wubiy1#

您的翻译有点错误。请尝试以下操作:
InteractionType.h

#ifndef InteractionTypeH
#define InteractionTypeH

#include <string>
#include <array>

class InteractionType {
    public:
        static const InteractionType PRINTACTIONS;
        static const InteractionType PRINTBANKNAME;
        static const InteractionType PRINTBANKACTIONS;
        static const InteractionType CREATEBRANCH;
        static const InteractionType CREATECUSTOMER;
        static const InteractionType READBRANCH;
        static const InteractionType READCUSTOMER;
        static const InteractionType UPDATEBRANCH;
        static const InteractionType UPDATECUSTOMER;
        static const InteractionType DELETEBRANCH;
        static const InteractionType DELETECUSTOMER;
        static const InteractionType QUITAPP;

        std::string toString() const;

        static std::array<InteractionType, 12> values();

    private:
        std::string description;
        InteractionType(std::string description);
};

#endif

InteractionType.cpp

#include "InteractionType.h"

const InteractionType InteractionType::PRINTACTIONS("Print Actions");
const InteractionType InteractionType::PRINTBANKNAME("Print Bank Name");
const InteractionType InteractionType::PRINTBANKACTIONS("Print Bank Actions");
const InteractionType InteractionType::CREATEBRANCH("Create a Branch");
const InteractionType InteractionType::CREATECUSTOMER("Create a Customer");
const InteractionType InteractionType::READBRANCH("Read Branches");
const InteractionType InteractionType::READCUSTOMER("Read Customers");
const InteractionType InteractionType::UPDATEBRANCH("Update a Branch");
const InteractionType InteractionType::UPDATECUSTOMER("Update a Customer");
const InteractionType InteractionType::DELETEBRANCH("Delete a Branch");
const InteractionType InteractionType::DELETECUSTOMER("Delete a Customer");
const InteractionType InteractionType::QUITAPP("Quit Application");

InteractionType::InteractionType(std::string description) {
    this->description = description;
}

std::string InteractionType::toString() const {
    return description;
}

std::array<InteractionType, 12> InteractionType::values() {
    return {
        PRINTACTIONS,
        PRINTBANKNAME,
        PRINTBANKACTIONS,
        CREATEBRANCH,
        CREATECUSTOMER,
        READBRANCH,
        READCUSTOMER,
        UPDATEBRANCH,
        UPDATECUSTOMER,
        DELETEBRANCH,
        DELETECUSTOMER,
        QUITAPP
    };
}

然后你可以这样使用它:

#include <iostream>
#include <limits>
#include "InteractionType.h"

void displayActions() {
    std::cout << "Actions: " << std::endl;
    size_t id = 0;
    for(auto &elem : InteractionType::values()) {
        std::cout << id++ << ':' << elem.toString() << std::endl;
    }
}

InteractionType getSelectedAction() {
    auto interactionTypes = InteractionType::values();
    std::cout << "Select an action: ";
    int id;
    std::cin >> id;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return interactionTypes.at(id);
}

Online Demo

相关问题