关于 :: 运算符

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;
}


...全文
120 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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啊有什么问题么?!
内容概要:本文详细介绍了一个基于C++语言开发的疫苗接种和儿童体检系统的设计与实现全过程,涵盖项目背景、目标意义、架构设计、核心功能模块、数据库实现、API接口规范、前后端代码实现及部署应用等多个方面。系统采用分层架构与模块化设计,实现了儿童信息管理、疫苗接种记录、体检数据录入、多用户权限控制、数据统计分析、异常预警、安全加密与日志审计等核心功能,并通过MySQL数据库进行数据持久化,结合Qt实现图形化界面,支持高并发、数据脱敏、多平台对接与自动化部署。项目强调数据安全、隐私保护与系统可扩展性,适用于社区医疗、疾控中心、学校及医院等场景。; 适合人群:具备C++编程基础,熟悉数据库操作和基本软件工程流程的开发人员、计算机及相关专业学生、医疗信息化项目开发者,以及希望了解完整医疗管理系统开发流程的技术人员。; 使用场景及目标:①学习如何使用C++构建完整的医疗信息管理系统;②掌握数据库设计、前后端交互、权限控制、多线程处理和GUI开发等关键技术;③应用于社区卫生服务、学校健康管理、疾控数据统计等实际业务场景,提升儿童健康管理水平;④作为课程设计、毕业设计或实际项目的参考模板。; 阅读建议:建议读者结合文档中的代码示例与数据库脚本,搭建本地开发环境进行实践操作,重点关注系统架构设计、模块解耦、安全机制与前后端交互逻辑,深入理解各功能模块的实现原理,并尝试在此基础上进行功能扩展,如接入移动端或增加AI分析模块。

65,199

社区成员

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

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