如何在c ++中åˆå§‹åŒ–大é‡çš„é™æ€ç±»

时间:2014-04-12 22:27:02

标签: c++

我有header.h

Class A
{
int var_1;
int var_2; 
};
Class b
{
private: static A var[MAX];
.....
};

但是如何在header.cpp中åˆå§‹åŒ–这个属性呢?

3 个答案:

答案 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;
}

http://ideone.com/ndkPmX