|
w*******e 发帖数: 285 | 2 比如我有一个class A,然后某个函数里初始化一个静态的A,
A* foo(){
static A a;
return &a;
}
如果有很多线程同时运行foo,那么结果会如何呢?我运行的结果似乎是无论有多少个线
程,a只被创建了一次。如果我在A的constructor里面sleep 10秒钟的话,那么只有一
个进程能够进入constructor并且sleep 10秒钟,而同时其他所有的线程就被lock在
static A a语句上了,直到constructor运行完毕才继续前进。是不是static A a本身
就能够实现线程间的同步,不需要额外加入mutex什么的?如果是的话,用这种简单方
法创建singleton有什么问题吗?谢谢了。 |
|
b***y 发帖数: 2799 | 3 ☆─────────────────────────────────────☆
qxc (睡觉自然醒, 数钱手抽筋) 于 (Tue Aug 23 18:18:35 2005) 提到:
compiler 老抱怨说我不能把一个 const 的 string 设置成为 static member,
VS2005.
这里面有什么逻辑原因吗? 多谢。
☆─────────────────────────────────────☆
aZhu (a+zhu) 于 (Tue Aug 23 18:23:43 2005) 提到:
I guess you initialize it inside the class.
i think you can't
though
class A{
static const int = 7;
// ..
}
is legal,
class A{
static const string a = "aaa";
//...
}
is not ba?
☆─────────────────────────────────────☆
yunhai (飞纵 |
|
f**********w 发帖数: 93 | 4 我当时是这样回答的,static变量存在内存的static data segment,虽然每个线程有自
己的local stack,但应该共享static variable,所以打印5。
当然drifter2008的回答要好得多。谢谢各位 |
|
e******0 发帖数: 211 | 5 From C++ primer
static 不属于对象,是属于累
virtual 是 base, dirived基于对象的绑定, 不适用于static
同理, static不能设置为const |
|
t****t 发帖数: 6806 | 6 virtual call depends on object. but in static call there is no object. how
does that make sense?
of course, you can call static method in virtual method. that's very fine.
but that doesn't means virtual-static make sense. |
|
P********e 发帖数: 2610 | 7 中文名字就比较好理解,一个叫多太,一个叫静态.
我觉得,static function可以互相call each other
但如果你的class B里面还有
virtual static void y();
x, y之间不能互相call, 就违背了static character |
|
j*p 发帖数: 115 | 8 I found this on the jobhunting board where someone posted his/her interview
process...
第三轮,又一个senior,请问什是多态?我答曰:其概念加C++实现的例子,虚函数若干
,此人追问什么是虚函数,我答曰,Vpr,Vt,等等。此人继续追问,解释vpr,vt的细
节实现,我努力解释。他还算满意,在问,什么是static,class里的static成员函数
和成员变量如何工作,我继续努力解释。他长叹了一口气,说:根据你以上解释,可不
可以有 static virtual function这个功能。我那个吐啊。。。我说这个不行的,一个
静态绑定,一个动态绑定,怎么玩。他说,这个可以玩,你看。。。扒拉扒拉(他开始
解释)。我真是觉得很无
语,难到我的那些解释不管用么?算了,看你是面试官,不计较了,我招了,我说这个
可以有。面试官再长叹一口气:这个真没有。我日啊。我问为什么,他说:这个就是没有
。然后继续一道,singleton pattern的编程题。
haha |
|
g**w 发帖数: 969 | 9 http://c2.com/cgi/wiki?VirtualStaticIdiom
class Foo
{
public:
Foo();
protected:
// Virtual static idiom <-- StaticCallback?()
virtual bool Callback();
private:
// Virtual static idiom --> Callback()
static bool StaticCallback?( void *pThis )
{
assert( pThis );
return ((Foo *)pThis)->Callback();
}
}; |
|
z****e 发帖数: 2024 | 10 "unnamed namespace中的全局变量"???
unnamed namespace 本来就是只有internal linkage,何谈全局?
"整数类型的static const没问题"???
用不着static,和整数,const默认就是internal linkage的。
“inline函数当中的static变量,就只能定义在头文件中了”???
定义在cpp中完全没有问题。 |
|
A**u 发帖数: 2458 | 11 继续请教一个
看more effective c++ P157
禁止在heap上创建object
class A{
private:
static void *operator new(size_t size);
};
这里 为什么也是static函数呢
请教大牛阿 我对这个static函数一点都没有认识
到底什么时候必须用 什么时候建议用 什么时候不能用呢
谢谢 |
|
r**u 发帖数: 1567 | 12 Here's a common scenario: source file first.cpp define a static data member
of a class and provided an initializer for it. Source file second.cpp #
includes first.h and attempts to examine the value of the static data member
. When you execute that statement, the program crashes.The reason is the
compiler doesn't guarantee that C::i is initialized before it is referenced
in the second.cpp. Here is example:
//----first.h
class C
{
public:
static const int i;
};
//----first.cpp
#include "first.... 阅读全帖 |
|
d****i 发帖数: 4809 | 13 Since C does not have class, static variable in C has different meanings
than in C++. So static variable in C means either 1) internal linkage (file
scope) 2) local static variable retains the value between function calls. |
|
b***i 发帖数: 3043 | 14 我认为C语言mess up了。
static居然有两个意思,一个是static lifetime(not local variable),一个是
intern storage(not extern)。
这个书讲的应该是说lifetime。你这里又不能写static,写了就在其他文件不可见了。 |
|
H******7 发帖数: 1728 | 15 否则就无法运行
比如这个, 去掉reverseWords的static就不对
public class Main {
public static String reverseWords(String s) {
StringBuilder reversed = new StringBuilder();
int j = s.length();
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == ' ') {
j = i;
} else if (i == 0 || s.charAt(i - 1) == ' ') {
if (reversed.length() != 0) {
reversed.append(' ');
}
reversed.append... 阅读全帖 |
|
S**Y 发帖数: 136 | 16 how is class static member variable implemented in c++?
how is a static variable of a function implemented in c++? |
|
t**g 发帖数: 1164 | 17 一道面试题
What is the difference between stack, static heap and dynamic heap?
我明白stack/heap的区别
可是啥叫static heap/dynamic heap?? |
|
s********a 发帖数: 1447 | 18 你是说
static heap和dynamic heap是分配了heap后 自己用数据结构实现的?
static heap用数组实现 dyanmic heap用树实现? |
|
H*X 发帖数: 281 | 19
我觉得是在问程序在内存里空间的占用吧
static heap或许是指static variables declared inside a function
然后dynamic 指普通malloc来的空间吧 |
|
S**I 发帖数: 15689 | 20 Global or static variables and objects have their storage allocated at
program startup, but may not be initialized until after the program has
begun executing. For instance, a static variable in a function is
initialized only the first time program execution passes through its
definition. |
|
p*********9 发帖数: 277 | 21 【 以下文字转载自 Programming 讨论区 】
发信人: powerplay09 (Roy), 信区: Programming
标 题: 问个static的问题
发信站: BBS 未名空间站 (Thu Mar 10 21:13:09 2011, 美东)
c和c++中的static有什么区别阿?
谢谢. |
|
a********m 发帖数: 15480 | 22 还有static也只在单分支递归能用。有两个以上分支就没戏了。参数比static好用而且
清楚多了。 |
|
r******n 发帖数: 12 | 23 My team is very actively look for candidates with solid C++/Compiler/static
program analysis skills sets.
I can be reached at
[email protected]
/* */
or call me at
5086473016
Thanks in advance.
Details can be found at this link:
http://www.mathworks.com/company/jobs/opportunities/c-software-
Job Summary
Our team is responsible for developing a brand-new cutting-edge verification
product, Simulink Code Inspector. We are looking for an experienced C++
developer with knowledge of program stat... 阅读全帖 |
|
c******n 发帖数: 4965 | 24 picasa displays the full size images in flash, so you can't obtain a static
link to your image. I don't like to use the "share" mechanism since that
makes your picasa ID public to viewers.
so is there a way to get a static link to your .jpg files on picasaweb?
thanks |
|
h****r 发帖数: 2056 | 25 I ask our internet provder to assign us a Static IP address, they ask me for a
port to take this address. which port is the best to have the Static IP
address.
Thanks |
|
s******e 发帖数: 63 | 26 you meant other classes 'remember' old final constants during their
compilation
time? does this only happen to 'final static'? so if I use 'static', it should
solve the problem?
problem is that I could not always do clean build on production. we need
to keep it running always and only patch couple of classes from time to time
(for example, constants in Constant class may be changed) |
|
m******t 发帖数: 2416 | 27
[snip]
I completely agree with what you said here and below about "final",
but unless I misunderstood the OP, his problem is with constants, i.e.,
"static final", rather than just "final". I would say they are totally
different animals. 8-)
Section 13.4.8, JLS actually does dictate that "compiler-time constants",
i.e., static final variables be inlined, and even gives some explanation
as to the rationale behind this requirement. |
|
m******t 发帖数: 2416 | 28 (for coconut, too)
See, I quoted the statement with "OO fundamentalist opinion" and "that's
just me." and we are still looking at a looming flame war. 8-)
But seriously, yes, static methods are convenient. I just thought that with
so many bad programmers out there, static methods have been so abused to the
point of causing way more damage than the benefits the convenience brings.
methods |
|
s******e 发帖数: 493 | 29 my $0.02.
If you are going to use spring anyway in ur project, you might want to think
tow things:
1. Do you see any chances in the future that the method/class is replaced or
reimplemented for any reasons (testing, production, etc)?
2. are the calls to the class/mothod scattered in ur code?
If either of them is true, I will go for the spring way.
Otherwise if it is just a generic utility class/method used in one or two
places, static might be good enough. I believe one benefit of static method |
|
i**p 发帖数: 902 | 30 想读点文章搞明白这种用法,用什么关键字到网上其搜索?
public class NotePadProvider extends ContentProvider {
....
private static final UriMatcher sUriMatcher;
......
static {
sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);
sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);
sNotesProjectionMap = new HashMap();
sNotesProjectionMap.put(Notes._ID, Notes._ID);
sNotesProjectionMap.put |
|
F****n 发帖数: 3271 | 31 Sorry that's my mistake.
static initializer is called when a class is loaded. That's why in case such
as JDBC, simply call
Class.forName("driverClassName")
can register the driver to DriverManager. They put driver registration code
in static initializer. |
|
b***i 发帖数: 3043 | 32 就是说,内部的private class xxx 里面要对外面的class的非static变量进行操作,
所以不能把xxx设static。
只能把这些本来就是常数的数组拿到外围。 |
|
l**********n 发帖数: 8443 | 33 i will fuck java if java has no static. static is handy
object. |
|
N********n 发帖数: 8363 | 34
You need static con to initialize static variables. |
|
g*****n 发帖数: 56 | 35 from effective C++ (2nd ed) item 28
struct widgets{
class widget {....};
static const Widget operator+(const Widget& lsh, const Widget& rhs);
....
};
I am wondering why declare a overloaded operator static? |
|
X****r 发帖数: 3557 | 36 I don't think you can overload operator + as a static member.
Quote:
13.5.2 Binary operators [over.binary]
1 A binary operator shall be implemented either by a non-static member
function (_class.mfct_) with one parameter or by a non-member function
with two parameters. Thus, for any binary operator @, x@y can be
interpreted as either x.operator@(y) or operator@(x,y). If both forms
of the operator function have been declared, the rul |
|
c********e 发帖数: 383 | 37 I wrote a small piece of code, if you run sbin, the out put is 0 however if
you run shbin the out put is one , which is what i expect...
can not figure out why. Thanks,
///////////////////////////////////////////
//lib.h
#include
#include
#include
template
class SingleTon
{
public:
static T * instance ();
protected:
SingleTon () {}
static T * instance_;
};
class A : public SingleTon
{
public:
A () {}
int size () { return box_.size (); }
void |
|
g****c 发帖数: 299 | 38 ☆─────────────────────────────────────☆
TFIIB (小明) 于 (Fri Aug 27 18:10:16 2004) 提到:
☆─────────────────────────────────────☆
thrust (哼哼的多隆猪) 于 (Fri Aug 27 20:30:27 2004) 提到:
ask yourself, if you have a "const" static member,
it is "const" in what sense??
☆─────────────────────────────────────☆
xiaozhu (體育用品) 于 (Fri Aug 27 21:26:22 2004) 提到:
why not? I can do that. for example:
public:
static const int test(){return 1;};
☆─────────────────────────────────────☆
thrust (哼哼的多隆猪) |
|
m******k 发帖数: 43 | 39 its lifetime is beyond function call, so must not have the stack rewind
destory the static variable.
maybe this is a dumb question, but why static variable can not be put into
stack section? |
|
t*******l 发帖数: 3662 | 40 really? who says "heap" and who says "stack".
the language itself does not specify any data segment for the static
variable as long as the behavior is correct.
in practice, many systems will put static variables into
the data segment (if initialized)
or the bss segment (if uninitialized). |
|
k****y 发帖数: 781 | 41 static argument 不可能吧,function parameter 都在stack上怎么可能static |
|
q*d 发帖数: 20 | 42 Thanks very much for confirmation!
I check some website, which said global and static are default to set to be
0. However, one thing confuse me is the term called:
BSS (from Block Started by Symbol). If you have unexplicitly initialized
globle var/static var set to 0, why you need BSS?
Also, does compiler automatically do this BSS for you?
(I am not cs major, so please forgive me, if this question is too naive:)
Thanks, |
|
c***d 发帖数: 996 | 43 ☆─────────────────────────────────────☆
paperdog (I've Never Been To Me) 于 (Tue Feb 13 14:48:35 2007) 提到:
what's the difference between singleton and class with static member
variables? and advantage of singleton, disadvantage of static member?
☆─────────────────────────────────────☆
magicfat (魔法胖子) 于 (Tue Feb 13 15:11:39 2007) 提到:
man, what kind of lame ass interview did you get yourself into? 8-)
☆─────────────────────────────────────☆
crystalike (Motivation) 于 (Tue Feb 13 15:15 |
|
n**d 发帖数: 9764 | 44 From "thinking in C++: "The typeid operator always produces a reference to a
static type_info object that describes the dynamic type of the object."
Why the following code output MI? Should it be Mi2? What does "static type_
info object" mean above?
*b2: 2MI
*mip: 2MI
*b1p: 2MI
mi2p not casted
#include
#include
#include
using namespace std;
class B2 {
public:
virtual ~B2() {}
};
class MI : public B2 {};
class Mi2 : public MI {};
void f(B2* &b2p)
{
delete b2p;
b2p |
|
c**a 发帖数: 316 | 45 class 的static 变量放在哪里?
Heap? stack?
另外 Heap 是不是 每个 process 只有一个
new 出来的都在哪里?
Global name space 的 static 变量在哪里?
各个name space 的呢?
谢谢 |
|
g*********8 发帖数: 53 | 46 uninitialized static var is on bss segment
initialized static var is on data segment |
|
b***y 发帖数: 2799 | 47 ☆─────────────────────────────────────☆
gebitan (job, job) 于 (Fri Sep 9 12:31:54 2005) 提到:
I am confusing what is the point to use static function here.
class Directory
{
string d_currentPath;
public:
static void preset(Directory &dir, char const *path);
};
void Directory::preset(Directory &dir, char const *newpath)
{
dir.d_currentPath = newpath; // ok
}
int main()
{
Directory dir;
|
|
x*******i 发帖数: 14 | 48 static DEFINE_MUTEX(dst_gc_mutex); //code is in linux/net/core/dst.c
question: in this line, static is used to define what? variable or function?
tks!
|
|
c******n 发帖数: 4965 | 49 class blah{
}
void fun() {
static blah myblah;
}
int main(int argc,char** argv) {
fun();
fun();
return 0;
}
你可以用g++ -S -o out.S mycode.cc
看看输出的assembly, compiler 自己生成了一个var 记录static var myblah 有否被
initialized 过 |
|
c***d 发帖数: 996 | 50 in c++:
Orange.h:
#include "fruit.h"
class Orange : public fruit {
}
Apple.h:
#include "fruit.h"
class Apple : public fruit {
}
fruit.h:
class fruit {
static string brand;
}
string fruit:brand = "dole";
if I compile this I will get multiple definition error at linking phrase. I
can not move the static variable definition to a cpp file since it is a
template file which i can not change. What would be a sensible way to work
around this? |
|