加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C中的箭头成员运算符

发布时间:2020-12-16 03:22:18 所属栏目:百科 来源:网络整理
导读:我很擅长使用C语言.我以前处理过 Java和ActionScript,但现在我想学习这种强大的语言.由于C授予程序员显式使用指针的能力,因此我对使用箭头成员运算符感到困惑.这是我尝试编写的示例代码. main.cpp中: #include iostream #include "Arrow.h" using namespace
我很擅长使用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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读