Yola
Yola

Reputation: 19041

Creating class/handle only from another class

I need only my mesh class to be able to create these classes, and i would like to not make these classes nested, because of too long qualified name if so. How can i do it best?

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); }
};

Only invalid handle should be accessible outside, for now i think it can be done by using static variables.

Upvotes: 1

Views: 39

Answers (1)

ForEveR
ForEveR

Reputation: 55897

Use 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
};

Upvotes: 1

Related Questions