简体   繁体   English

我可以在 C++ 中将类成员变量定义为多种数据类型吗?

[英]Can I define a class member variable in C++ be multiple data types?

So I have a class definition, and I want to add a member variable that can be 1 of 2 different classes depending on the operating system this code is run on.所以我有一个类定义,我想添加一个成员变量,它可以是 2 个不同类中的 1 个,具体取决于运行此代码的操作系统。

Is there anyway to do this in C++ so that I can initialize a different class for the "operating_system" member variable depending on some arguement or variable when initializing MyOperatingSystem?无论如何在 C++ 中这样做,以便我可以在初始化 MyOperatingSystem 时根据一些争论或变量为“operating_system”成员变量初始化一个不同的类?

#include <iostream>
#include "Win.h"
#include "Lin.h"

using namespace std;

typedef int os_type;
enum {Win, Lin};

class MyOperatingSystem {
  public:
    MyOperatingSystem(int ver, string n, os_type os);
  private:           
    int version;
    string name;
    // operating_system // want this to be either (Windows win | Linux lin)

};

// constructor
MyOperatingSystem::MyOperatingSystem(int ver, string n, os_type os){
    version = ver;
    name = n;
    if (os == Win){
        // operating system = Windows(int i);
    }
    else{
        // operating system = Linux(int i)
    }
}

Win.h and Lin.h are as follows Win.h和Lin.h如下

Win.h:赢.h:

#include <windows.h>
class Windows{
    public:
        Windows(int i){
            integer = i;
            mystring = "WinString";
        }
    private:
        int integer;
        LPCWSTR mystring;
};

Lin.h:林.h:

#include <termios.h>
class Linux{
    public:
        Linux(int i){
            integer = i;
            mystring = "LinString";
        }
    private:
        int integer;
        cc_t* mystring;
};

I suggest making a compile-time decision.我建议在编译时做出决定。

Example:例子:

#pragma once // MyOperatingSystem.h

class IOperatingSystem {
public:
    virtual ~IOperatingSystem() = default;

    // misc operations:
    virtual foo() = 0;
};

#ifdef _WIN32
#include "internal/Win.h" // in here MyOperatingSystem  implements  IOperatingSystem 
#else
#include "internal/Lin.h" // in here MyOperatingSystem  implements  IOperatingSystem 
#endif

You don't necessarily need virtual here but it helps when designing to make sure that both implementations follow the same interface.这里不一定需要virtual ,但在设计时它有助于确保两个实现都遵循相同的接口。

最明显的解决方案是拥有一个具有通用接口的基本OperatingSystem类,并从中派生出您的WinLinux

The operating system is a compile time thing so you can use a template parameter操作系统是编译时的东西,因此您可以使用模板参数

template <ostype os> class MyOperatingSystem ...

You can then use if constexpr to check which os you have and include different code in the class.然后,您可以使用if constexpr检查您拥有的操作系​​统并在类中包含不同的代码。 The Windows and Linux classes should probably have a common base class and then most of the differences would be solved by virtual methods. WindowsLinux类可能应该有一个共同的基类,然后大部分差异将通过虚拟方法解决。 So if constexpr (os == Win) ... should be the exception.因此if constexpr (os == Win) ...应该是例外。

Another way to go would be a dependency insertion:另一种方法是依赖插入:

MyOperatingSystem(int ver, string n, Os & os);

where Os is the common base class for Windows and Linux .其中OsWindowsLinux的通用基类。

Or again, since this is a compile time thing:或者再次,因为这是编译时的事情:

template <typename Os> class MyOperatingSystem : public Os { ... }
template <typename Os> class MyOperatingSystem  { ... Os os; }

In this case you don't even need a common base class (but it helps to ensure a common interface).在这种情况下,您甚至不需要通用基类(但它有助于确保通用接口)。 Both Windows and Linux just have to follow a common interface, like defining an os_type . WindowsLinux都只需要遵循一个通用接口,比如定义一个os_type If Os::os_type is a constexpr you could use if constexpr again.如果Os::os_type是一个 constexpr,你可以再次使用if constexpr But I think the point of the Os classes is to encapsulate all the OS specific code so again that should be rare if used at all.但我认为 Os 类的重点是封装所有特定于操作系统的代码,所以如果使用的话应该很少见。

Note that with the Os template way the MyOperatingSystem class is (or has) a concrete Windows or Linux class.请注意,使用Os模板方式, MyOperatingSystem类是(或具有)具体的WindowsLinux类。 So all the virtual function calls get replaced by concrete calls.所以所有的虚函数调用都被具体调用所取代。 So no overhead.所以没有开销。 The earlier cases have a pointer or reference to the base class and will need to do virtual method calls.较早的案例有一个指向基类的指针或引用,并且需要进行虚方法调用。

std::variant<Windows, Linux> operating_system;

std::variant is a sum type -- it is one of the types in the list of types. std::variant是 sum 类型——它是类型列表中的类型之一。

As a side effect, it carries with it the type it is -- so the os_type variable is redundant to store beside it.作为副作用,它带有它的类型——所以os_type变量存储在它旁边是多余的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 定义多个C++类成员函数 - Define multiple C++ class member function C ++超类为子类定义静态成员变量 - c++ super class define static member variable for subclass 在哪里定义用于 C++ 中 class 的模板成员变量的常量? - Where to define constants for use in a template member variable of a class in C++? 如何在 class 中定义构造函数,并将结构作为 c++ 中的数据成员? - How to define constructor in class with struct as data member in c++? 在C ++中,我可以在其范围之外定义私有类的指针成员吗? - In C++, can I define a pointer-to-member of a private class outside its scope? 用C ++优雅的方式在成员变量中保存不同类型的数据 - C++ elegant way of holding different types of data in a member variable 我可以使用SFINAE在模板类中有选择地定义成员变量吗? - can I use SFINAE to selectively define a member variable in a template class? 在 C++ 中,我可以创建一个线程并将其保存为类成员变量,以便它可以在析构函数中自动加入吗? - In C++, can I create a thread and save it as a class member variable so it can be joined automatically in the destructor? C ++将成员变量传递给成员类 - C++ Passing a member variable to a member class 我可以在C ++类中将成员变量声明为const吗? - Can i declare member variable as const in class of c++?if yes,how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM