0

I am suppose to reduce it down to [request->headers().getMethodValue())].

I am fairly new to 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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Do you know what pointers are? `.` is just a member access and `->` is the same, just for pointers. Modern IDEs can assist you in keeping track of types of expressions – Lukas-T Jun 18 '20 at 07:03
  • These look like chained|piped function and member calls (my C++ is rusty). AS @churill mentioned you need to find out how pointers and member functions work and then how to chain them (use the results of the last command for the next action). This isn't always great practice in C++ as a function may not return the expected result. A free C++ course one youtube you could try: https://www.youtube.com/watch?v=_bYFu9mBnr4&list=PL_c9BZzLwBRJVJsIfe97ey45V4LP_HXiG – Anthony Jun 18 '20 at 08:38

3 Answers3

0

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. I suggest you study about object-oriented programming styles, especially polymorphism to understand this kind of code.

K.R.Park
  • 1,015
  • 1
  • 4
  • 18
0

The . operator is used to access stuff inside the object (functions, variables etc.) the -> operator is just a . operator but for pointers. In your case you call request's headers function that returns an object. For that object then you call getMethodValue function.

SzymonO
  • 432
  • 3
  • 15
0

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

Test* tPtr;

tPtr->print() // prints i;

to access object;

Test tObj;

tObj.print() // print i;
pvc
  • 1,070
  • 1
  • 9
  • 14