关于 :: 运算符

ZXW0521 2012-07-04 04:46:33
在ace的代码中,发现了如下代码
size_t const len = ::wcslen (wstr) + 1;

这里的::是代表什么意思,我google后发现有个类似问题,说这种::不带东西的表示全局作用域,但我还是搞不清楚什么意思


在ctime中,也有类似的:
#undef clock
#undef difftime
#undef mktime
#undef time
#undef asctime
#undef ctime
#undef gmtime
#undef localtime
#undef strftime

namespace std
{
using ::clock_t;
using ::time_t;
using ::tm;

using ::clock;
using ::difftime;
using ::mktime;
using ::time;
using ::asctime;
using ::ctime;
using ::gmtime;
using ::localtime;
using ::strftime;
}


...全文
108 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZXW0521 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 4 楼 的回复:
全局作用域指的范围和小范围作用域各是什么,我有点弄不懂。


全局作用域就是不处于任何一个 {} 之外的. 一对 { } 之间的内容处于同一个局部作用域中.
[/Quote]

明白了,
包括函数,类,namespace什么的,
以前有些混淆,
www_adintr_com 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
全局作用域指的范围和小范围作用域各是什么,我有点弄不懂。
[/Quote]

全局作用域就是不处于任何一个 {} 之外的. 一对 { } 之间的内容处于同一个局部作用域中.
ZXW0521 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 2 楼 的回复:

C/C++ code

int x = 10;

int main()
{
int x = 1;
printf("%d, %d", x, ::x); // 1, 10
}


有点理解了,但是还有些不明白:
size_t const len = ::wcslen (wstr) + 1;
这句代码所在的文件,和<ctime>一样,最终都是被……
[/Quote]
你没程序上下文看,这个问题有点为难了,我自己的rtfsc吧。
谢谢了。
ZXW0521 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

查MSDN是Windows程序员必须掌握的技能之一。
mk:@MSITStore:D:\MSDN98\98VS\2052\vclang.chm::/html/_pluslang_c.2b2b_.operators.htm
C++ Operators
Operators specify an evaluation to be performed on one of the following……
[/Quote]
感谢回复,我的问题是在centos上编译时遇到的,所以没去看ms的东西,
赵4老师 2012-07-04
  • 打赏
  • 举报
回复
mk:@MSITStore:D:\MSDN98\98VS\2052\vccore.chm::/html/_langref_scope_resolution_operator.3a_3a3a.htm
Scope Resolution Operator: ::
In C++, you can tell the compiler to use the global variable rather than the local variable by prefixing the variable with ::, the scope resolution operator.

If you have nested local scopes, the scope resolution operator doesn't provide access to variables in the next outermost scope. It provides access to only the global variables.

Example

// Example of the scope resolution operator
#include <iostream.h>
int amount = 123; // A global variable
void main()
{
int amount = 456; // A local variable
cout << ::amount; // Print the global variable
cout << '\n';
cout << amount; // Print the local variable
}
The example has two variables named amount. The first is global and contains the value 123. The second is local to the main function. The two colons tell the compiler to use the global amount instead of the local one.
赵4老师 2012-07-04
  • 打赏
  • 举报
回复
查MSDN是Windows程序员必须掌握的技能之一。
mk:@MSITStore:D:\MSDN98\98VS\2052\vclang.chm::/html/_pluslang_c.2b2b_.operators.htm
C++ Operators
Operators specify an evaluation to be performed on one of the following:

One operand (unary operator)


Two operands (binary operator)


Three operands (ternary operator)
The C++ language includes all C operators and adds several new operators. Table 1.1 lists the operators available in Microsoft C++.

Operators follow a strict precedence which defines the evaluation order of expressions containing these operators. Operators associate with either the expression on their left or the expression on their right; this is called “associativity.” Operators in the same group have equal precedence and are evaluated left to right in an expression unless explicitly forced by a pair of parentheses, ( ). Table 1.1 shows the precedence and associativity of C++ operators (from highest to lowest precedence).

Table 1.1 C++ Operator Precedence and Associativity

Operator Name or Meaning Associativity
:: Scope resolution None
:: Global None
[ ] Array subscript Left to right
( ) Function call Left to right
( ) Conversion None
. Member selection (object) Left to right
–> Member selection (pointer) Left to right
++ Postfix increment None
–– Postfix decrement None
new Allocate object None
delete Deallocate object None
delete[ ] Deallocate object None
++ Prefix increment None
–– Prefix decrement None
* Dereference None
& Address-of None
+ Unary plus None
– Arithmetic negation (unary) None
! Logical NOT None
~ Bitwise complement None
sizeof Size of object None
sizeof ( ) Size of type None
typeid( ) type name None
(type) Type cast (conversion) Right to left
const_cast Type cast (conversion) None
dynamic_cast Type cast (conversion) None
reinterpret_cast Type cast (conversion) None
static_cast Type cast (conversion) None
.* Apply pointer to class member (objects) Left to right
–>* Dereference pointer to class member Left to right
* Multiplication Left to right
/ Division Left to right
% Remainder (modulus) Left to right
+ Addition Left to right
– Subtraction Left to right
<< Left shift Left to right
>> Right shift Left to right
< Less than Left to right
> Greater than Left to right
<= Less than or equal to Left to right
>= Greater than or equal to Left to right
== Equality Left to right
!= Inequality Left to right
& Bitwise AND Left to right
^ Bitwise exclusive OR Left to right
| Bitwise OR Left to right
&& Logical AND Left to right
|| Logical OR Left to right
e1?e2:e3 Conditional Right to left
= Assignment Right to left
*= Multiplication assignment Right to left
/= Division assignment Right to left
%= Modulus assignment Right to left
+= Addition assignment Right to left
–= Subtraction assignment Right to left
<<= Left-shift assignment Right to left
>>= Right-shift assignment Right to left
&= Bitwise AND assignment Right to left
|= Bitwise inclusive OR assignment Right to left
^= Bitwise exclusive OR assignment Right to left
, Comma Left to right

ZXW0521 2012-07-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C/C++ code

int x = 10;

int main()
{
int x = 1;
printf("%d, %d", x, ::x); // 1, 10
}
[/Quote]

有点理解了,但是还有些不明白:
size_t const len = ::wcslen (wstr) + 1;
这句代码所在的文件,和<ctime>一样,最终都是被其它cpp include的,
这里::wcslen 是全局作用域的 wcslen,而不是某个小作用域的 wcslen,
全局作用域指的范围和小范围作用域各是什么,我有点弄不懂。

ZXW0521 2012-07-04
  • 打赏
  • 举报
回复
对于这里的using含义没什么问题,
我只是对 ::前面不加东西不理解,

using ::clock 这里带空格的,
using::clock 估计编译不过去吧,
这是 ::前不带东西,如果是表示全局的话,那么加::和不加 ::是不是就是一个意思了
www_adintr_com 2012-07-04
  • 打赏
  • 举报
回复

int x = 10;

int main()
{
int x = 1;
printf("%d, %d", x, ::x); // 1, 10
}

vanxeger 2012-07-04
  • 打赏
  • 举报
回复
就是全局作用域啊!!
using::clock
表示using里面的clock啊有什么问题么?!

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧