我很擅长使用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.