C++ 成员运算符

C++ 教程 · 2019-02-25 13:27:30

.(点)运算符和 ->(箭头)运算符用于引用类、结构和共用体的成员。

点运算符应用于实际的对象。箭头运算符与一个指向对象的指针一起使用。例如,假设有下面的结构:

struct Employee {
  char first_name[16];
  int  age;
} emp;

(.)点运算符

下面的代码把值 "zara" 赋给对象 emp 的 first_name 成员:

strcpy(emp.first_name, "zara");

(->)箭头运算符

如果 p_emp 是一个指针,指向类型为 Employee 的对象,则要把值 "zara" 赋给对象 emp 的 first_name 成员,需要编写如下代码:

strcpy(p_emp->first_name, "zara");

-> 称为箭头运算符,它是由一个减号加上一个大于号组成。

简而言之,访问结构的成员时使用点运算符,而通过指针访问结构的成员时,则使用箭头运算符。

点击查看所有 C++ 教程 文章: https://www.codercto.com/courses/l/18.html

查看所有标签

A Byte of Python

A Byte of Python

Swaroop C H / Lulu Marketplace / 2008-10-1 / USD 27.98

'A Byte of Python' is a book on programming using the Python language. It serves as a tutorial or guide to the Python language for a beginner audience. If all you know about computers is how to save t......一起来看看 《A Byte of Python》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具