与“静态”混淆 [英] confused with "static"

查看:98
本文介绍了与“静态”混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的新闻组,


我在某种程度上与静态关键字的使用相混淆。


我可以看到两个功能关键字static与数据一起使用

类的成员。


1.数据成员在此类的所有对象中引用相同的数据

或者换句话说,通过使用static关键字,一个类的所有对象

可以共享数据。 (这就是我想要的)


2.数据成员只能访问或仅存在。在该文件中

该类被宣布


问题现在出现了。当我想使用静态

关键字的1函数时我在这里描述了

但是想要访问静态数据成员的成员函数是

在另一个文件中写(如果你想创建一个库,那就像它的b / b
那么有用)然后就会出现这样的问题:

这个文件是静态数据成员<不能接受
,这会导致链接错误。?


只是一个问题,为这个问题创建关键字不是更好

第一个函数静态在意义上

曾经在内存中创建静态而第二个函数看起来像是外部的oposide

关键字?


感谢您的帮助


亚历山大·马尔



解决方案

Alexander Mahr写道:

亲爱的新闻组,

我在某种程度上混淆了static关键字的用法。

我可以看到关键字static的两个功能与一个类的
数据成员一起使用。

1.数据成员在这个对象中引用类到相同的数据或者换句话说使用static关键字的所有对象都可以共享数据。 (这就是我想要的)


这是一个。只存在该变量的一个实例(通常在类的实现文件中为
)和所有类实例(对象)

访问该变量。

2.数据成员仅可访问或仅存在。在文件中
其中类声明为


定义。你将它与全局(命名空间级别)变量声明混合起来。

问题现在出现了。当我想使用我在这里描述的
静态关键字的1函数时,但是想要访问静态数据成员的成员函数是在另一个文件中写的(就像它的<如果你想创建一个库,那么有用吗?那就有问题
在这个文件中,静态数据成员不能被接收,这会导致链接错误。


不。如果A类有一个静态成员calles stat_member它可以被访问

作为A :: stat_member(如果它是公共的或你是朋友等等 - 所以它是

accessibler )来自外部的并从内部作为stat_member(成员

函数)。所有成员函数需要看的是类

声明。而且他们还是需要看看。

只是一个问题,为第一个函数静态创建一个关键字并不是更好。意思是
曾经在内存中创建静态而第二个函数
看起来像是extern
关键字的oposide?


它会的。静态关键字严重过载。有4个

含义:静态命名空间级别函数和变量(2),静态成员

变量和静态局部变量。


-

WW aka Attila


WW WW,


自你知道关于关键字static的更多关于我的信息

请允许我举个例子。首先是示例代码


################文件1:CTest.h


class CTest

{

public:

static int static_data_member;


CTest(){} ;

虚拟~CTest(){};

void memberfunction();

}

### ##############文件2:CTest.cpp


#include" CTest.h"


void CTest :: memberfunction()

{

static_data_member = 1; //这种访问是否可行?我读静态会

限制

//访问只是在

文件中静态数据成员

//被宣布?

}


#############示例结束

/>
这段代码是否会编译并且没有错误?


因为我得到一个类似的代码


谢谢WW,

CU Alexander




" Alexander Mahr" <毫安****** @ gmx.de>在消息中写道

news:bk ************* @ news.t-online.com ...

亲爱的新闻组,

我在某种程度上与静态关键字的使用相混淆。

我可以看到关键字static的两个功能与数据的结合
成员1.数据成员在该类的所有对象中引用相同的数据
或者换句话说,通过使用static关键字,一个类的所有对象都可以共享数据。 (这就是我想要的)

2.数据成员只能访问或仅存在。在
文件中,类声明为


它也可能意味着函数变量在函数调用中持续存在。

问题现在出现了。当我想使用我在这里描述的静态
关键字的1函数时,但是想要访问静态数据成员的成员函数是在另一个文件中写的(就像它的<如果你想创建一个库,那么有用的话就是存在这样的问题:
这个文件中静态数据成员不能被接收,这会导致链接错误。


不,当你为类成员变量使用静态时,它是一个不同的用法

并不意味着文件范围。你不应该有这个问题。

只是一个问题,为
第一个函数静态创建关键字不是更好吗?意思是
曾经在内存中创建静态而第二个函数是
看起来像是extern
关键字的oposide?


有些人这么认为。


Dear Newsgroup,

I''m somehow confused with the usage of the static keyword.

I can see two function of the keyword static in conjunction with a data
member of a class.

1. The data member reffers in all objects of this class to the same data
Or in other word by using the static keyword all objects of one class
can share data. (This is what I want)

2. The data member is only accesable or "only exists" within the file where
the class is declared

The problem is now. That when I want to use the 1 function of the static
keyword I discribed here
but the member functions, which want to acces the static data member are
writen in another file (like its
usefull if you want to create a library) then there is the problem that in
this file the static data member
can''t be accesed which results in a link error.?

Just a question wouldn''t it have been better to create a keyword for the
first function "static" in the meaning
of once created in then static in memory and the second function which seems
like the oposide of the extern
keyword?

Thank you for your help

Alexander Mahr



解决方案

Alexander Mahr wrote:

Dear Newsgroup,

I''m somehow confused with the usage of the static keyword.

I can see two function of the keyword static in conjunction with a
data member of a class.

1. The data member reffers in all objects of this class to the same
data Or in other word by using the static keyword all objects of
one class can share data. (This is what I want)
This is the one. Only one instance of that variable should exist (usually
in the implementation file for the class) and all class instances (objects)
access that one.
2. The data member is only accesable or "only exists" within the file
where the class is declared
Defined. You mix it up with global (namespace level) variable declarations.
The problem is now. That when I want to use the 1 function of the
static keyword I discribed here
but the member functions, which want to acces the static data member
are writen in another file (like its
usefull if you want to create a library) then there is the problem
that in this file the static data member
can''t be accesed which results in a link error.?
Nope. If class A has a static member calles stat_member it can be accessed
as A::stat_member (if it is public or you are friend etc. - so it is
accessibler)from the "outside" and as stat_member from the inside (member
functions). All what the member functions need to see is the class
declaration. And that they need to see anyways.
Just a question wouldn''t it have been better to create a keyword for
the first function "static" in the meaning
of once created in then static in memory and the second function
which seems like the oposide of the extern
keyword?



It would. The static keyword is severely "overloaded". There are those 4
meanings: static namespace level functions and variables (2), static member
variables and static local variables.

--
WW aka Attila


Hi WW,

Since you know lot more like me about the keyword static please
allow me to ask you with an example. First the example Code

################ File 1 : CTest.h

class CTest
{
public:
static int static_data_member;

CTest(){};
virtual ~CTest(){};
void memberfunction();
}
################# File 2: CTest.cpp

#include "CTest.h"

void CTest::memberfunction()
{
static_data_member=1; //Is this acces possible ? I read static would
limit
//acces to be just within the
file of where the static data member
//is declared?
}

############# end of example

Would this code compile and like without an error?

Since I get one with an similar code

Thank you WW,
CU Alexander



"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...

Dear Newsgroup,

I''m somehow confused with the usage of the static keyword.

I can see two function of the keyword static in conjunction with a data
member of a class.

1. The data member reffers in all objects of this class to the same data
Or in other word by using the static keyword all objects of one class
can share data. (This is what I want)

2. The data member is only accesable or "only exists" within the file where the class is declared
It also can mean that function variables persist across function calls.
The problem is now. That when I want to use the 1 function of the static
keyword I discribed here
but the member functions, which want to acces the static data member are
writen in another file (like its
usefull if you want to create a library) then there is the problem that in
this file the static data member
can''t be accesed which results in a link error.?
No, when you use static for class member variables, it''s a different usage
and doesn''t imply file scope. You shouldn''t have that problem.
Just a question wouldn''t it have been better to create a keyword for the
first function "static" in the meaning
of once created in then static in memory and the second function which seems like the oposide of the extern
keyword?



Some people think so.


这篇关于与“静态”混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆