s*******o 发帖数: 392 | 1 本人仍在继续学习cplusplus。com网站的教程,
看到了static member这一章节,
这是例子code
#include
using namespace std;
class CDummy
{
public:
static int n;
CDummy(){ n++ ;};
~CDummy(){ n--; };
};
int CDummy::n = 0;
int main()
{
CDummy a;
CDummy b[5];
CDummy*c = new CDummy;
cout << b[2].n <
cout << c->n <
delete c;
cout << CDummy::n <
cout << b->n <
system("PAUSE");
return 0;
}
我的问题是问什么在delete c之前n输出值为7, 而在delete c之后的输出值时6?大家
能不能
试一试?而且我不懂从哪里来的 这个值, 7?
谢谢各位大虾了。 |
g*********s 发帖数: 1782 | 2 ctor CDummy was called 7 times. so n = 7.
afterwards, dtor ~CDummy was called once. so n = 6.
u should read some texbooks first. it seems ur understanding on c++ is
very elementary.
try c++ prime or the c++ programming language, both latest version.
【在 s*******o 的大作中提到】 : 本人仍在继续学习cplusplus。com网站的教程, : 看到了static member这一章节, : 这是例子code : #include : using namespace std; : class CDummy : { : public: : static int n; : CDummy(){ n++ ;};
|
s*******o 发帖数: 392 | |
M7 发帖数: 219 | 4 IMHO, the C++ programming language并不适合初学用.
【在 s*******o 的大作中提到】 : You honor, Domino!
|
s*******o 发帖数: 392 | 5 懂了,多谢前辈,我在快速的看cplusplus的tutorial,下一步估计就是primer |
X****r 发帖数: 3557 | 6 IMHO,这本书十分适合已经会写程序的(比如已经会了C)初学C++。
如果还不会别的语言的话,C++本身就不太适合作为第一个语言。
【在 M7 的大作中提到】 : IMHO, the C++ programming language并不适合初学用.
|
W*********y 发帖数: 481 | 7 http://www.learncpp.com/cpp-tutorial/811-static-member-variable
看看这个tutorial就明白static member了
【在 s*******o 的大作中提到】 : 本人仍在继续学习cplusplus。com网站的教程, : 看到了static member这一章节, : 这是例子code : #include : using namespace std; : class CDummy : { : public: : static int n; : CDummy(){ n++ ;};
|
f******y 发帖数: 2971 | 8 What compiler do you use? Can this compile?
cout << b[2].n <
cout << c->n <
【在 s*******o 的大作中提到】 : 本人仍在继续学习cplusplus。com网站的教程, : 看到了static member这一章节, : 这是例子code : #include : using namespace std; : class CDummy : { : public: : static int n; : CDummy(){ n++ ;};
|
S**I 发帖数: 15689 | 9 why not?
【在 f******y 的大作中提到】 : What compiler do you use? Can this compile? : cout << b[2].n <: cout << c->n <
|
g*********s 发帖数: 1782 | 10 oh, i didn't notice it. but i thought Class::static_var was the only right
way.
【在 S**I 的大作中提到】 : why not?
|
|
|
t****t 发帖数: 6806 | 11 \you need some elementary c++ textbook as well...
【在 g*********s 的大作中提到】 : oh, i didn't notice it. but i thought Class::static_var was the only right : way.
|
g*********s 发帖数: 1782 | 12 actually no patience to go through those thick textbooks anymore, although
i agree refreshing the memory is always good. most of time cplusplus.com
is enough.
on the other hand, lz obviously has no idea about static data member in a
class.
【在 t****t 的大作中提到】 : \you need some elementary c++ textbook as well...
|
s*******o 发帖数: 392 | 13 多谢各位的指导,找工作,我估计会看看c++ primer,然后再读读code,大概这样。没
想到大家讨论的这么热烈! |
t*********t 发帖数: 61 | 14 静态变量的定义和使用在C++的入门教科书中都有介绍,lz应该直接从网上搜一下。太
基础了,不知找哪类工作面试。实话说,这段code我也理解困难,怀疑该网站的水平。 |
t****t 发帖数: 6806 | 15 这是一段很简单的tutorial code, 目的很清楚, 很容易理解. 如果对这段code理解困
难的话, 你应该怀疑自己的水平.
【在 t*********t 的大作中提到】 : 静态变量的定义和使用在C++的入门教科书中都有介绍,lz应该直接从网上搜一下。太 : 基础了,不知找哪类工作面试。实话说,这段code我也理解困难,怀疑该网站的水平。
|
g*********s 发帖数: 1782 | 16 哈哈,你最近火气有点大呀。
cplusplus.com还是很不错的。
【在 t****t 的大作中提到】 : 这是一段很简单的tutorial code, 目的很清楚, 很容易理解. 如果对这段code理解困 : 难的话, 你应该怀疑自己的水平.
|