如何在class中声明和初始化一个静态成员?
How to declare and initialize a static member in a class?
当我编译包含以下头文件的代码时,我收到一条错误消息:
Graph.h:22: error: ISO C++ forbids in-class initialization of non-const
static member `maxNumberOfNeighbors'
如何声明和初始化不是 const 的静态成员?
这是 .h 文件
#ifndef GRAPH_H
#define GRAPH_H
typedef char ElementType;
class Graph {
public:
class Node {
public:
static int maxNumberOfNeighbors = 4;;
int numberOfNeighbors;
Node * neighbors;
ElementType data;
Node();
Node(ElementType data);
void addNeighbor(Node node);
};
typedef Node* NodePtr;
Graph();
void addNode(Node node);
NodePtr getNeighbors(Node node);
bool hasCycle(Node parent);
private:
NodePtr nodes;
static int maxNumberOfNodes;
int numberOfNodes;
};
#endif /* GRAPH_H */
最简单的做法是遵循错误消息的建议。如果它抱怨 non-const static,请将其设置为 const。
static int const maxNumberOfNeighbors = 4;
特别是考虑到无论如何它应该是一个常量,按照它的名字。您不会更改最大值,对吧!?
否则,如果你打算改变它,只在class定义之外初始化和定义它。
// At namespace scope, in one file
int Graph::Node::maxNumberOfNeighbors = 4;
class Node {
public:
static const int maxNumberOfNeighbors = 4;
};
一个人绝对可以有 class 不是 CV-qualified 的静态成员(非 const 且非 volatile)。只是根据当前的 ISO C++ 规定,当在 class 中声明它们时,不应初始化它们(赋予它们值)。在比较中,对于非静态数据成员这样做是可以的(不管CV-qualification)因为C++11.
因为静态数据成员不属于任何对象,所以可以在 class 之外分配它们(如果它们不是常量,则可以对其进行操作)(记住右作用域运算符)。此外,无论 public/private 声明和 CV-qualification,静态数据成员都可以在其 class.
之外初始化
因此,初始化静态数据成员的一种方法是在相同的 block-scope/namespace 中执行此操作,其中 classes(在 sub-classes 的情况下为外部 class)位于,但不在任何 class 范围内。
例如:
class Graph {
public:
class Node {
public:
static int maxNumberOfNeighbors;
.
.
.
};
.
.
.
};
int Graph::Node::maxNumberOfNeighbors = 4;
//also int Graph::Node::maxNumberOfNeighbors(4);
祝你好运!
当我编译包含以下头文件的代码时,我收到一条错误消息:
Graph.h:22: error: ISO C++ forbids in-class initialization of non-const
static member `maxNumberOfNeighbors'
如何声明和初始化不是 const 的静态成员?
这是 .h 文件
#ifndef GRAPH_H
#define GRAPH_H
typedef char ElementType;
class Graph {
public:
class Node {
public:
static int maxNumberOfNeighbors = 4;;
int numberOfNeighbors;
Node * neighbors;
ElementType data;
Node();
Node(ElementType data);
void addNeighbor(Node node);
};
typedef Node* NodePtr;
Graph();
void addNode(Node node);
NodePtr getNeighbors(Node node);
bool hasCycle(Node parent);
private:
NodePtr nodes;
static int maxNumberOfNodes;
int numberOfNodes;
};
#endif /* GRAPH_H */
最简单的做法是遵循错误消息的建议。如果它抱怨 non-const static,请将其设置为 const。
static int const maxNumberOfNeighbors = 4;
特别是考虑到无论如何它应该是一个常量,按照它的名字。您不会更改最大值,对吧!?
否则,如果你打算改变它,只在class定义之外初始化和定义它。
// At namespace scope, in one file
int Graph::Node::maxNumberOfNeighbors = 4;
class Node {
public:
static const int maxNumberOfNeighbors = 4;
};
一个人绝对可以有 class 不是 CV-qualified 的静态成员(非 const 且非 volatile)。只是根据当前的 ISO C++ 规定,当在 class 中声明它们时,不应初始化它们(赋予它们值)。在比较中,对于非静态数据成员这样做是可以的(不管CV-qualification)因为C++11.
因为静态数据成员不属于任何对象,所以可以在 class 之外分配它们(如果它们不是常量,则可以对其进行操作)(记住右作用域运算符)。此外,无论 public/private 声明和 CV-qualification,静态数据成员都可以在其 class.
之外初始化因此,初始化静态数据成员的一种方法是在相同的 block-scope/namespace 中执行此操作,其中 classes(在 sub-classes 的情况下为外部 class)位于,但不在任何 class 范围内。
例如:
class Graph {
public:
class Node {
public:
static int maxNumberOfNeighbors;
.
.
.
};
.
.
.
};
int Graph::Node::maxNumberOfNeighbors = 4;
//also int Graph::Node::maxNumberOfNeighbors(4);
祝你好运!