我很擅长使用C语言.我以前处理过
Java和ActionScript,但现在我想学习这种强大的语言.由于C授予程序员显式使用指针的能力,因此我对使用箭头成员运算符感到困惑.这是我尝试编写的示例代码.
main.cpp中:
#include <iostream> #include "Arrow.h" using namespace std; int main() { Arrow object; Arrow *pter = &object; object.printCrap(); //Using Dot Access pter->printCrap(); //Using Arrow Member Operator return 0; }
Arrow.cpp
#include <iostream> #include "Arrow.h" using namespace std; Arrow::Arrow() { } void Arrow::printCrap(){ cout << "Steak!" << endl; }
在上面的代码中,它所做的只是使用两种方法(Dot和Arrow)打印牛排.
简而言之,在使用C编写实际应用程序时,何时使用箭头符号?由于我以前的编程经验,我习惯使用点符号,但箭头对我来说是全新的.
解决方法
好问题,
Dot(.) this operator is used for accessing the member function or
sometime the data member of a class or structure using instance
variable of that class/Structure.
object.function(); object.dataMember; //not a standard for class.
arrow(->) this operator is used for accessing the member function or
sometime the data member of a class or structure but using pointer of
that class/Structure.
ptr->function(); ptr->datamember; //not a standard for class.
原文地址:https://www.jb51.cc/c/111702.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。