Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将类声明为整数 类测试{ 公众: 静态int n; test(){n++;}; ~test(){n--;}; }; int测试::n=0//_C++_Class_Initialization_Colon_Scope Resolution - Fatal编程技术网

C++ 将类声明为整数 类测试{ 公众: 静态int n; test(){n++;}; ~test(){n--;}; }; int测试::n=0//

C++ 将类声明为整数 类测试{ 公众: 静态int n; test(){n++;}; ~test(){n--;}; }; int测试::n=0//,c++,class,initialization,colon,scope-resolution,C++,Class,Initialization,Colon,Scope Resolution,从该声明中— class test { public: static int n; test () { n++; }; ~test () { n--; }; }; int test::n=0; //<----what is this step called? how can a class be declared as an integer? int main () { test a; test b[5]; // I'm not sure wha

从该声明中—

class test {
public:
    static int n;
    test () { n++; };
    ~test () { n--; };
};

int test::n=0; //<----what is this step called? how can a class be declared as an integer?

int main () {
    test a;
    test b[5]; // I'm not sure what is going on here..is it an array?
    test * c = new test;
    cout << a.n << endl;
    delete c;
    cout << test::n << endl;
}
”称为作用域解析运算符。此运算符用于初始化静态字段n,而不是语句中的类

class test {
public:
    static int n;
    test () { n++; };
    ~test () { n--; };
};

int test::n=0; //<----what is this step called? how can a class be declared as an integer?

int main () {
    test a;
    test b[5]; // I'm not sure what is going on here..is it an array?
    test * c = new test;
    cout << a.n << endl;
    delete c;
    cout << test::n << endl;
}
”称为作用域解析运算符。此运算符用于初始化静态字段n,而不是语句中的类

class test {
public:
    static int n;
    test () { n++; };
    ~test () { n--; };
};

int test::n=0; //<----what is this step called? how can a class be declared as an integer?

int main () {
    test a;
    test b[5]; // I'm not sure what is going on here..is it an array?
    test * c = new test;
    cout << a.n << endl;
    delete c;
    cout << test::n << endl;
}
”称为作用域解析运算符。此运算符用于初始化静态字段n,而不是语句中的类

class test {
public:
    static int n;
    test () { n++; };
    ~test () { n--; };
};

int test::n=0; //<----what is this step called? how can a class be declared as an integer?

int main () {
    test a;
    test b[5]; // I'm not sure what is going on here..is it an array?
    test * c = new test;
    cout << a.n << endl;
    delete c;
    cout << test::n << endl;
}

”称为作用域解析运算符。此运算符用于初始化静态字段n,而不是类中声明的类

静态数据成员。它们是在类之外定义的

因此在类定义中

int test::n=0; 
记录

class test {

public:
static int n;
test () { n++; };
~test () { n--; };
};
只有声明n。您需要定义它,即为它分配内存。 还有这个

这是它的定义<代码>测试::n是表示n属于类测试的变量的限定名称

根据类定义,当类的对象被构造时,这个静态变量会增加

int test::n=0;
当一个对象被破坏时,这个静态变量就会减少

test () { n++; };
事实上,这个静态变量起着计算类的活动对象的作用

因此,您主要使用名称a定义了类的对象

~test () { n--; };
每次定义对象时,都会调用该类的构造函数。因此,n增加并等于1。 Adter定义5个对象的数组

test a;
n等于6

动态分配一个或多个对象后

test b[5];  
n等于7

明确删除后

test * c = new test;

n再次变为6,因为被调用的析构函数减少了n。

静态数据成员在类中声明。它们是在类之外定义的

因此在类定义中

int test::n=0; 
记录

class test {

public:
static int n;
test () { n++; };
~test () { n--; };
};
只有声明n。您需要定义它,即为它分配内存。 还有这个

这是它的定义<代码>测试::n是表示n属于类测试的变量的限定名称

根据类定义,当类的对象被构造时,这个静态变量会增加

int test::n=0;
当一个对象被破坏时,这个静态变量就会减少

test () { n++; };
事实上,这个静态变量起着计算类的活动对象的作用

因此,您主要使用名称a定义了类的对象

~test () { n--; };
每次定义对象时,都会调用该类的构造函数。因此,n增加并等于1。 Adter定义5个对象的数组

test a;
n等于6

动态分配一个或多个对象后

test b[5];  
n等于7

明确删除后

test * c = new test;

n再次变为6,因为被调用的析构函数减少了n。

静态数据成员在类中声明。它们是在类之外定义的

因此在类定义中

int test::n=0; 
记录

class test {

public:
static int n;
test () { n++; };
~test () { n--; };
};
只有声明n。您需要定义它,即为它分配内存。 还有这个

这是它的定义<代码>测试::n是表示n属于类测试的变量的限定名称

根据类定义,当类的对象被构造时,这个静态变量会增加

int test::n=0;
当一个对象被破坏时,这个静态变量就会减少

test () { n++; };
事实上,这个静态变量起着计算类的活动对象的作用

因此,您主要使用名称a定义了类的对象

~test () { n--; };
每次定义对象时,都会调用该类的构造函数。因此,n增加并等于1。 Adter定义5个对象的数组

test a;
n等于6

动态分配一个或多个对象后

test b[5];  
n等于7

明确删除后

test * c = new test;

n再次变为6,因为被调用的析构函数减少了n。

静态数据成员在类中声明。它们是在类之外定义的

因此在类定义中

int test::n=0; 
记录

class test {

public:
static int n;
test () { n++; };
~test () { n--; };
};
只有声明n。您需要定义它,即为它分配内存。 还有这个

这是它的定义<代码>测试::n是表示n属于类测试的变量的限定名称

根据类定义,当类的对象被构造时,这个静态变量会增加

int test::n=0;
当一个对象被破坏时,这个静态变量就会减少

test () { n++; };
事实上,这个静态变量起着计算类的活动对象的作用

因此,您主要使用名称a定义了类的对象

~test () { n--; };
每次定义对象时,都会调用该类的构造函数。因此,n增加并等于1。 Adter定义5个对象的数组

test a;
n等于6

动态分配一个或多个对象后

test b[5];  
n等于7

明确删除后

test * c = new test;
n再次变为等于6,因为被调用的析构函数减少了n