C++ question: static
登录 | 论坛导航 -> 华新鲜事 -> 社会百科 | 本帖共有 11 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-04 11:32:31  楼主  关注此帖
C++ question: static
Expert, 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.
Never frown even though tomorrow may be grey, may be blue
Who knows who will love your smile in next minute
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:VeryGood (等级:6 - 驾轻就熟,发帖:738) 发表:2003-06-04 11:55:41  2楼 评分:
my thots
a 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.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-04 12:53:31  3楼
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.
followon question
Thanks, 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
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-06-04 14:35:00  4楼 评分:
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
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.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-04 15:54:50  5楼
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.
1-yes, 2-no
I 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?
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-04 16:08:07  6楼
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.
OS: Sun Solaris 2.6
And i use g++ menu1.cpp -o menu1 command line compiled.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:Flying (等级:18 - 华新水车,发帖:16849) 发表:2003-06-04 16:13:48  7楼 评分:
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?
I saw your problem. But I don't think that's the
point.

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.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-05 10:10:24  8楼
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.
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
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-05 10:23:58  9楼
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.
Seems to be a reason
But i have not fully understood the explanation yet. And Can someone summary size the use of static?
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:VeryGood (等级:6 - 驾轻就熟,发帖:738) 发表:2003-06-05 15:09:33  10楼
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
only a static member can be initialize in this way
you should initialized it in you constructor instead.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
作者:graduating (等级:4 - 马马虎虎,发帖:562) 发表:2003-06-06 10:13:46  11楼
only a static member can be initialize in this wayyou should initialized it in you constructor instead.
Many thanks to you two. This post encloses
this question.
Thanks again.
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版只看此人从这里展开收起列表
论坛导航 -> 华新鲜事 -> 社会百科 | 返回上一页 | 本主题共有 11 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码