我有header.h
Class A
{
int var_1;
int var_2;
};
Class b
{
private: static A var[MAX];
.....
};
但是如何在header.cppä¸åˆå§‹åŒ–这个属性呢?
ç”案 0 :(得分:3)
ç»™Aä¸€ä¸ªé»˜è®¤çš„æž„é€ å‡½æ•°ï¼Œå°†æ•´æ•°è®¾ç½®ä¸º0或者什么。
A b::var[MAX] = {};
ç”案 1 :(得分:0)
å‡è®¾class A
å…¬å…±æž„é€ å‡½æ•°æ²¡æœ‰å‚æ•°ï¼ˆæˆ–é»˜è®¤æž„é€ å‡½æ•°ï¼‰ï¼š
文件 b.h :
#include "a.h"
// global constant:
const int MAX = 10;
class B {
private:
static A var[MAX]; // declaration
};
文件 b.cpp :
#include "b.h"
A B::var[MAX] = {}; // definition
ç”案 2 :(得分:0)
在实施档案ä¸ï¼š
A b::var[] = {}; // static member definition
// A() is called for each member so you might want
// to implement it
示例:
#define MAX 10
class A
{
int var_1;
int var_2;
};
class b
{
private:
static A var[MAX]; // static member declaration
};
A b::var[] = {}; // static member definition
int main() {
return 0;
}