-3

I am new to C++, recently I saw a code as a follows

 if (A1 -> B1) A1->B1->C1(numbers);

I have difficulty to understand this simple line. Could you please advise me? I guess, it says, if pointer A1 is assigned to B1(true) then point A1 to B1 and B to function C1, am I right? Thanks for your help

kmsin
  • 3
  • 1

1 Answers1

0

there is a class or struct somewhere in your code. unlike. operator -> is used to access a member of a struct or a class which is pointed by a pointer. let's assume our classes looks something like this;

class Foo
{
public:
  void C1(int numbers);
};

class Bar
{
public:
  Foo* B1;    //pointer to B1 object.
};

let's also assume somewhere in the code we had a pointer declared and we try to access the member of object that is pointed by the pointer.

Foo* myFoo; //pointer myFoo is declared. 
myFoo->C1; //trying to access C1 function what  myFoo is pointing at.

in the above example, we are trying to access the member of myFoo with ->. The problem is though myFoo doesn't point to anything.. we only declared the pointer but not assigned to an address of any particilarFoo object. Therefore, using -> operator to access the pointer members would cause undefined behaviour. So in order to combat this possible problem that may occur in the code, we can guard the myFoo with a nullptr check before accessing it's member

Foo* myFoo = nullptr;

we can now check if myFoo is valid or not with.

if(myFoo)
   myFoo->C1(1); //if its valid call the C1 function.

above code accesses the member of myFoo only if myFoo is valid.

going back to your example we can now assume what's going on.

       if(A1->B1) //check if the B1 pointer of the A1 is valid.
          A1->B1->C1(3); //if it is valid, call the C1 function of the object B1 pointing to.
Yucel_K
  • 688
  • 9
  • 17