登录 | 首页 -> 华新鲜事 -> 社会百科 | 切换到:传统版 / sForum | 树形列表
C++ question: static
<<始页  [1]  末页>> 

C++ question: staticExpert, i have a question on static.
The partial code is as following:

[quote]
// in a class Object
...
private:
typedef void (Object::*Omf) ();
static Omf farray[3];
...
[/quote]

Here i can't understand why "static" is a must?
I have tried to remove "static" but then i get compilation error message:

menu1.cpp:17: 'void (Object::* Object::farray[3]()' is not a static member of 'class Object'

Here static seems not a lifetime issue. Thanks for you enlightenmen.
[graduating (6-4 11:32, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

my thotsa static member is a class member, while a non-static member is an boject member.

It is hard for the compiler to check validity of the sytax if the member fuction poiter is an object memeber. because the compiler cannot always know the exact memeber function it is pointing to at compile time.
[VeryGood (6-4 11:55, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 VeryGood:my thotsa static member is a class member, while a non-static member is an boject member. It is hard for the compiler to check...)followon questionThanks, VeryGood. The compiler really complains about non-static member functions as shown in error message.

My followon question is that: in a class defintion, there are acutally many non-static member function. Here the member function is private, does it matter? Is it possible to define a non-static private member function?

TIA
[graduating (6-4 12:53, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

(引用 graduating:followon questionThanks, VeryGood. The compiler really complains about non-static member functions as shown in error message. ...)It seems that you are confused...1) The sample you gave above is NOT a declaration of member function. Instead, the typdef statement simply aliases a data type of member function pointer as "Omf" for the ease of reference.

2) No matter farray is declared as static or not, the syntax is acceptable! That is, there is NO compelling reason that farray must be declared static. Please double check.
[Flying (6-4 14:35, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

(引用 Flying:It seems that you are confused...1) The sample you gave above is NOT a declaration of member function. Instead, the typdef state...)1-yes, 2-noI agree with your first point. It is really just aliasing. I had mis-used some words such as static functions. It is not private function. farray is a pointer to array of functions. farray is a private member of class Object.
So the problem is that the keyword static is not allowed to be omitted, otherwise compiler complains as shown in the error message
[quote]
menu1.cpp:17: 'void (Object::* Object::farray[3]()' is not a static member of 'class Object'
[/quote]
So you see the problem?
[graduating (6-4 15:54, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 Flying:It seems that you are confused...1) The sample you gave above is NOT a declaration of member function. Instead, the typdef state...)OS: Sun Solaris 2.6And i use g++ menu1.cpp -o menu1 command line compiled.
[graduating (6-4 16:08, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 graduating:1-yes, 2-noI agree with your first point. It is really just aliasing. I had mis-used some words such as static functions. It is ...)I saw your problem. But I don't think that's thepoint.

I compiled the following code using g++ version 2.95.2 on Solaris:

class Object {
private:
typedef void (Object::*Omf)();
/*static*/ Omf farray[3];
};

Compilation gave no error. I suppose the error was caused by other factors.
[Flying (6-4 16:13, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼

(引用 Flying:I saw your problem. But I don't think that's thepoint. I compiled the following code using g++ version 2.95.2 on Solaris: clas...)Other source of error?Flying, i investigate it again. I think the problem may caused by the initialization of farray.

In the public part, i defined some public functions, and i initialize the farray with these public functions.

Class Object
{
public:
void retrieve() {...}
void insert() {...}
void update() {...}

private:
typedef void (Object::*Omf)();
Omf farray[3];
};

Object::Omf Object::farray[3]=
{
&Object::retrieve, &Object::insert, &Object::update
};

&Object::retrieve and such are fixed addresses, so farray must be defined as static, correct? Is this a rule defining a static member?
What are the rules for definition of static members?

TIA
[graduating (6-5 10:10, Long long ago)] [ 传统版 | sForum ][登录后回复]8楼

(引用 VeryGood:my thotsa static member is a class member, while a non-static member is an boject member. It is hard for the compiler to check...)Seems to be a reasonBut i have not fully understood the explanation yet. And Can someone summary size the use of static?[graduating (6-5 10:23, Long long ago)] [ 传统版 | sForum ][登录后回复]9楼

(引用 graduating:Other source of error?Flying, i investigate it again. I think the problem may caused by the initialization of farray. In the p...)only a static member can be initialize in this wayyou should initialized it in you constructor instead.[VeryGood (6-5 15:09, Long long ago)] [ 传统版 | sForum ][登录后回复]10楼

(引用 VeryGood:only a static member can be initialize in this wayyou should initialized it in you constructor instead.)Many thanks to you two. This post enclosesthis question.
Thanks again.
[graduating (6-6 10:13, Long long ago)] [ 传统版 | sForum ][登录后回复]11楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 社会百科 | [刷新本页] | 切换到:传统版 / sForum