简体   繁体   English

如何理解 c++ 中这样的代码 [request->headers().Method()->value().getStringView())]

[英]How to understand code like this in c++ [request->headers().Method()->value().getStringView())]

I am suppose to reduce it down to [request->headers().getMethodValue())] .我想把它减少到[request->headers().getMethodValue())]

I am fairly new to C++.我对 C++ 相当陌生。 Can someone please tell me how to understand this type of code?有人可以告诉我如何理解这种类型的代码吗? There are multiple .有多个. operator and -> operator.运算符和->运算符。 I loose track of the classes and others.我忘记了班级和其他人。

It's a little overwhelming.这有点压倒性。

Actually, those type of codes are common in languages that supports object-oriented programming.实际上,这些类型的代码在支持面向对象编程的语言中很常见。

The most likely reason is that if combined well with class hierarchy and inheritance, the single line reduces many if-else statement to terse syntax like one you mentioned.最可能的原因是,如果与 class 层次结构和 inheritance 很好地结合使用,则单行将许多 if-else 语句减少为像您提到的那样的简洁语法。 I suggest you study about object-oriented programming styles, especially polymorphism to understand this kind of code.我建议你学习面向对象编程styles,尤其是多态性来理解这种代码。

The . . operator is used to access stuff inside the object (functions, variables etc.) the -> operator is just a .运算符用于访问 object 内部的东西(函数、变量等) ->运算符只是一个. operator but for pointers.运算符,但用于指针。 In your case you call request 's headers function that returns an object.在您的情况下,您调用requestheaders function 返回 object。 For that object then you call getMethodValue function.对于 object,然后调用getMethodValue function。

Answers already explained, what for .答案已经解释过了,为什么. operator and -> operator;运算符和->运算符;

an example would be;一个例子是;

class Test
{
    public:
      print( int i) { std::cout << i << std::endl; }
};

to access pointer object访问指针 object

Test* tPtr;

tPtr->print() // prints i;

to access object;访问 object;

Test tObj;

tObj.print() // print i;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM