gpt4 book ai didi

c++ - 仅从另一个类创建类/句柄

转载 作者:行者123 更新时间:2023-11-30 05:40:47 25 4
gpt4 key购买 nike

我只需要我的网格类就可以创建这些类,我不想让这些类嵌套,因为如果是这样的话,限定名太长了。我怎样才能做到最好?

struct Handle
{
explicit Handle(int i) : _index(i) {}
bool IsValid() { return _index != NO_HANDLE; }

protected:
enum { NO_HANDLE = -1 };
int _index;
};

// **************************************************************************************
struct HalfEdgeHandle : Handle
{
HalfEdgeHandle(int i) : Handle(i) {}
static HalfEdgeHandle GetInvalid() { return HalfEdgeHandle(NO_HANDLE); }
};

// **************************************************************************************
struct FaceHandle : Handle
{
FaceHandle(int i) : Handle(i) {}
static FaceHandle GetInvalid() { return FaceHandle(NO_HANDLE); }
};

// **************************************************************************************
struct VertexHandle : Handle
{
VertexHandle(int i) : Handle(i) {}
static VertexHandle GetInvalid() { return VertexHandle(NO_HANDLE); }
};

外部只能访问无效的句柄,目前我认为可以通过使用静态变量来完成。

最佳答案

使用 friend 。

class Factory;

struct Handle
{
protected:
explicit Handle(int i) : _index(i) {}
public:
bool IsValid() { return _index != NO_HANDLE; }

protected:
enum { NO_HANDLE = -1 };
int _index;
friend class Factory; // and so on in Derived classes
};

关于c++ - 仅从另一个类创建类/句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31429810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com