->和::的问题请教?

GP625 2008-11-11 12:27:35
如题 -> ::
有哪位大哥可以指导一下这两个符号在什么时候用啊
最好可以有例子 加注释更好
本人只是知道在容器里见过双冒号 "->" 大家给我讲解下吧
我想学习学习
所以想请教论坛的大哥们
...全文
121 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅得不敢出门 2008-11-11
  • 打赏
  • 举报
回复
The member access operators . and -> are used to refer to members of structures, unions, and classes

// expre_Selection_Operator.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct Date {
Date(int i, int j, int k) : day(i), month(j), year(k){}
int month;
int day;
int year;
};

int main() {
Date mydate(1,1,1900);
mydate.month = 2;
cout << mydate.month << "/" << mydate.day
<< "/" << mydate.year << endl;

Date *mydate2 = new Date(1,1,2000);
mydate2->month = 2;
cout << mydate2->month << "/" << mydate2->day
<< "/" << mydate2->year << endl;
delete mydate2;
}

wzfxyer 2008-11-11
  • 打赏
  • 举报
回复
class cal
{
};
cal *pt;
pt->
cal ob;
ob.
::域运算符。
帅得不敢出门 2008-11-11
  • 打赏
  • 举报
回复
两个不能混为一谈啊.
::作用域
You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
:: identifier
class-name :: identifier
namespace :: identifier
例子:

#include <iostream>

using namespace std;

int amount = 123; // A global variable

int main() {
int amount = 456; // A local variable
cout << ::amount << endl // Print the global variable
<< amount << endl; // Print the local variable
}


-> 这个是由结构指针访问成员时用的
The operator for allowing member access.



P_ghost 2008-11-11
  • 打赏
  • 举报
回复
指针使用“->”,域使用“::”
  • 打赏
  • 举报
回复
在较多的情况下用到这两个。如结构体指针,类对象指针,引用名字空间,引用类的静态变量等。

可能没有人能帮你,得自己学习,在学习中积累!
R9R9R9 2008-11-11
  • 打赏
  • 举报
回复
c++ primer
Mougou 2008-11-11
  • 打赏
  • 举报
回复
很复杂,还是自己看书的好。
qingkongyihe2008 2008-11-11
  • 打赏
  • 举报
回复
指针操作和域操作
使用的地方很多,如果lz 想灵活运用的话必须多联系,多看实例,多总结
GP625 2008-11-11
  • 打赏
  • 举报
回复
谢谢各位啊 小弟有些明白了~~
感谢了
cyj626 2008-11-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zmlovelx 的回复:]
两个不能混为一谈啊.
::作用域
You can tell the compiler to use the global identifier rather than the local identifier by prefixing the identifier with ::, the scope resolution operator.
:: identifier
class-name :: identifier
namespace :: identifier
例子:

C/C++ code
#include <iostream>

using namespace std;

int amount = 123; // A global variable

int main() {
int amou…
[/Quote]

up
qq675927952 2008-11-11
  • 打赏
  • 举报
回复
c++ primer
tnfyj 2008-11-11
  • 打赏
  • 举报
回复
->操作符:
简单的来说就是通过一个复合类型(类,结构体等)的指针变量来引用其内部成员的手段

比如 class AClass{private: int a}
定义一个AClass类 它有一个数据成员为int型
那么定义一个指向AClass类的指针 AClass *pa;
就可以通过->来访问a这个数据成员了 pa->a

建议楼主如果真的想学的话,找本C++的书好好看一下
frisky_lobo 2008-11-11
  • 打赏
  • 举报
回复
calss A:public C
A.B
从属关系
frisky_lobo 2008-11-11
  • 打赏
  • 举报
回复
还有个单冒号的,比如
A = B ? C : D;
A:D(){};

65,211

社区成员

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

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