d****p 发帖数: 685 | 1 Haha, :-) These days excellent interviewees are always way better than
mediocre interviewers like me.
Looked like I made seriously false statements in the above example.
The static member variable in the class Fruit has external linkage. It will
bear internal linkage if it is qualified by const. A non class static
variable however bears internal linkage.
standard 3.5 defines all the rules - enjoy reading the mess! |
|
z****e 发帖数: 2024 | 2 namespace scope static means external linkage.
这个真是学到了。但是没有namescope的static都是internal的吧。我是这样认为的。
另外,当virtual要出现多态的时候,inline就失效了。通过object调用,inline成立。 |
|
f**********w 发帖数: 93 | 3 void foo()
{ static int i;
// ......
}
假设我有这样一个static变量,如果有两个线程按如下时间顺序调用foo(),
T0:第一个线程把i设为3,
T1:第二个把i设为5,
T2:第一个线程打印i,i的值应该是3,然后两个线程退出foo(),
T3:现在i的值应该是多少?
这个过程编译器是如何对i进行操作的?
请指教 |
|
G****A 发帖数: 4160 | 4 即使不声明为static,无论建立多少个instances,class function始终只keep一个copy。
既然这样,请问为什么只有被声明为static function 才能 be called without an
instance of the class. |
|
h*******s 发帖数: 8454 | 5 不是static的函数调用的时候系统自己给牵头加上一个this指针作为第一个参数
如果没有instance,哪儿来得this指针啦
不是static的函数可以修改instance自己的私有变量啊,虽然只有一个,但是从哪里调
用还是有区别的
copy。 |
|
e****d 发帖数: 895 | 6 Declaration allows name lookup to find the entity.
Definition defines the entity.
A definition can also be a declaration.
You declare a static data member in a class and define it outside the class.
One definition rule doesn't allow you define a static data member
in a class becuase multiple includes will violate the ODR. |
|
e****d 发帖数: 895 | 7 extern int x = 0 is a definition.
Initializer means definition.
When you define a function inside a class, it's inline, which
doesn't violate the ODR.
If there are two translation units and each of them include
the class with a static data member defined in the class,
there will be two instances of this static data member in
the program, which violates ODR. const integral data member
is a special case, by which the compiler might just put the
member inside the class definition. |
|
t****t 发帖数: 6806 | 8 people often found that c++ has a lot of things to remember. but in fact, if
you know the reason behind them, there is actually very little to "remember
".
For example, in this case, there is only one thing to remember, which is "
one definition rule".
* extern: if you declare it extern, you most probably have definition
elsewhere. so the one with extern is not definition. however if there is
initializer, then this must be the definition, since only one initializer is
allowed.
* function w/o bod... 阅读全帖 |
|
b*****e 发帖数: 474 | 9 这一段:
* static data member: class definition is meant to be included in multiple
files. if static data member is definition, then one definition rule is
violated. therefore you must defined it ("again") somewhere else.
- 这个 #include 不是问题吧, 用 #ifndef 。。。 #define 。。。#endif
保证了只一次的
if
remember
is
somewhere |
|
g*********s 发帖数: 1782 | 10 so if i define a class w/ static variable members in foo.h, i'll have to
create a foo.cpp file to keep the definitions of these static variables,
instead of put these definitions in foo.h?
fact, if
"remember
is "
is
initializer is
somewhere |
|
s*******o 发帖数: 392 | 11 本人仍在继续学习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?大家
能不能
试一试?而且... 阅读全帖 |
|
|
g*********s 发帖数: 1782 | 13
is
virtual). it
pointer,
is
assume such an implementation to support static virtual:
for b->x(), we first know "this" has dynamic type D, then we lookup
class D's static method list to find x().
here i don't see any problem on prototype uniqueness...
of course, the principle of programming board is "trust is always right
on c++" and i honor it. :) but i just want to have a deeper
understanding through the discussion. |
|
m*********g 发帖数: 646 | 14 I really don't want to argue with ppl on BBS. Ok, whatever you say.
I just assume ppl are experienced here, and know an empty static vector is
useless except showing some correct "book knowledge".
But thanks for your input. I know the way to use a static array but still it
helps, especially abt the c++OX.
have
doesn
may
. |
|
m*********g 发帖数: 646 | 15 I really don't want to argue with ppl on BBS. Ok, whatever you say.
I just assume ppl are experienced here, and know an empty static vector is
useless except showing some correct "book knowledge".
But thanks for your input. I know the way to use a static array but still it
helps, especially abt the c++OX.
have
doesn
may
. |
|
r****t 发帖数: 10904 | 16 这个地方 vector A::v = A::f();
这一句没有像你说的一样写在 main() 里面,是笔误,还是就应该在外面?
member
struct A
{
static vector v;
static vector f(){ vector v; v.push_back("123"); v.push_back
("abc"); return v;}
};
vector A::v = A::f();
int main()
{
cout << A::v[0] << " " << A::v[1] << endl;
}. |
|
|
a***e 发帖数: 30 | 18 static void foo( boolean * validPtr )
谢谢。 |
|
F********g 发帖数: 475 | 19 If it's not explicitly initialized(static int temp;), stored in .bss.
If it is(static int temp=0x10;) ,stored in .data |
|
r****t 发帖数: 10904 | 20 静态类是啥?static class? 还是 static object? |
|
l***i 发帖数: 1309 | 21 Is there a way to access static variables within a function in C/C++?
Specifically, I was trying to do this:
int calc(int n)
{
static cnt;
cnt++;
// do something
}
but sometimes I need to reset cnt to 0 from main()
I know there are alternatives like global or use a class wrapper. Just want
to know if there is a solution that does almost no extra work.
Many thanks |
|
i**p 发帖数: 902 | 22 I think this is author's mistake. In the context he says "the order of
initialization of static objects across translation units", but the code
example is non-static global object. In short, the example doesn't match the
context. |
|
i**p 发帖数: 902 | 23 Thanks! We all agree it addresses the "global" meanings of the variable.
Because the "static" in C++ has both storage and linkage meanings, I prefer
to use "global" in stead of the "static" in this context. It is less
confusing.
label |
|
f*z 发帖数: 421 | 24 大牛,同学们,请教,static member method 可不可用default argument?比如,
enum Index
{
INDEX_A = 0,
INDEX_B
};
class foo
{
public:
static void method1( int a, int b = Index::INDEX_A);
};
编译可以过,但有什么要注意的地方?
多谢! |
|
i****0 发帖数: 1 | 25 有个类似关节转动的system
运动频率不高,有个变化的外力使其转动
我想知道转动的同时,关节连接出的变形和应力场
请问这个是用quasi-static还是dynamic?
似乎quasi-static就可以把?这两个有什么区别的优缺点么?
谢谢了! |
|
b*********4 发帖数: 3542 | 26 quasi-static 一般是驱动频率小于系统固有频率的三分之一,这时候不需要考虑系统
的惯性力。 取决于你的系统,如果你的系统结构比较结实,一般可以认为是quasi-
static的。 |
|
b***k 发帖数: 2673 | 27 ☆─────────────────────────────────────☆
ccca (cc) 于 (Wed Jul 2 16:25:35 2008) 提到:
发信人: ccca (cc), 信区: Programming
标 题: Re: C++: operator new 为啥要是 static的, 不是有啥影响?
发信站: BBS 未名空间站 (Wed Jul 2 16:25:26 2008), 转信
class A{
public:
void* operator new(size_t){A* p=this;};
}
编译器如是说,
d:\documents\visual studio 2005\projects\xx1\xxx1\xxx1.cpp(20
) : error C2671: 'A::operator new' : static member functions do not have
'this' pointers
☆─────────────────────────────────────☆
binrose (BigHead) 于 ( |
|
C***m 发帖数: 120 | 28 1. I prefer static replication to dynamic replication, as long as it is
possible. If one cannot perfectly replicate the final payoff using simple
static replication, then go dynamic.
2. (a) market liquidity,可能找不到足够多的量来复制,比如ATM digital option
(b)dynamic 只能replicate 一些greeks. 比如delta hedge,可能会有gamma risk.
(c) dynamic 多少需要模型来决定。模型的risk
胡说几句,大牛们轻拍。
that
the |
|
|
|
|
|
|
|
t*****e 发帖数: 102 | 35 protected void setPasscodeGridView(int layoutId, View view) {
if(mPasscodeGV==null) { initUI(view); }
PasscodeGridAdapter adapter = new PasscodeGridAdapter(getActivity(),
numbers, layoutId);
mPasscodeGV.setAdapter(adapter);
mPasscodeGV.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
if (holdButtonPress)
... 阅读全帖 |
|
x***y 发帖数: 633 | 36 This is what I got by goole:
Single* obtain_single()
{
void* p = get_system_pool();
byte_t* i = static_cast(p);
if (*(i-1) == 0)
{
Single* s = new(p) Single;
return s;
}
return static_cast(p);
}
Basically, It's the same with using static data member except using the "
get_system_pool()" to count how many objects have been instantiated....
Anyone knows what this function is? Or how to code it? Or any better
methods to do the singleton? |
|
f****4 发帖数: 1359 | 37 void f(){
static int i;
{//上锁
if(i%2==0)
++i;
else
i+=2;
}//解锁
}
如果是多线程的话 用啥来加锁?有专门的库和函数么? |
|
s******9 发帖数: 84 | 38 基于数组的Heap是Static的,因为容量无法扩展。基于树的Heap是dynamic的。 |
|
t**g 发帖数: 1164 | 39 这里的heap其实已经超越了C++本身范畴把?
int *a=new int[100] //这里是static heap?
能举一个用dynamic heap的C++代码语句么? |
|
y**i 发帖数: 1112 | 40 我也不知道楼主的问题指的是什么,不过且不说这个,static变量不是被分配在全局区
/静态区么,heap区不是说是需要程序员自己释放空间,全局区会在程序结束时自动释
放么 |
|
w*********l 发帖数: 1337 | 41 那样的话heap根本不可能是dynamic的。都是固定地址空间。是heap就要被malloc (或
者衍生的C++的new等等)。
你说的这个static variables其实跟global一样,是在bss(block started by symbol)
,跟heap一点儿关系都没有。 |
|
w*********l 发帖数: 1337 | 42 说得基本没错。但是heap在程序死亡的时候也会被释放。所有进程占用的资源都会被释
放。memory leak不是因为heap不释放,是因为usage会一直增长不降。static空间根本
不会增长,所以也不用担心释放的问题。 |
|
n***r 发帖数: 105 | 43 static variable是store在data segment里面的 |
|
c**********e 发帖数: 2007 | 44 What type of linkage does global variables and functions preceded by the
storage class specifier static have?
a) Internal
b) Intern
c) External
d) Extern |
|
C***y 发帖数: 2546 | 45 static STL container一般编译器放在哪个segment中?
如果一直push的话,重新new的内存又在哪里?
谢谢 |
|
g*********s 发帖数: 1782 | 46 what he asked is, if vector X is a global/static variable, where the
vector data are put. it is not about X's allocation, but X's _M_data. |
|
c***2 发帖数: 838 | 47 In C++, static also means there is only one instance of that (variable/
method).
in both c and C++
1) scope: local to this file
2) retains value in function calls
3) memory is allocated at compile time on data section instead of runtime on
stack |
|
|
t****t 发帖数: 387 | 49 我听说static在c++中文件范围的作用和c是一样的
当然对于member是另一回事 |
|
U*********y 发帖数: 54 | 50 写recursive的程序时, 经常会需要建某个variable或data structure,然后多次用到它
并需要它maintain value between calls. 一般有两种方法可以选择, 一种可以在
recursive的函数里declare它static; 另一种是在函数的wrapper里declare它然后pass by
reference.
一直想知道哪种方法更好,大家有没有什么见解? |
|