C++ - Difference between (*). and ->?

后端 未结 3 1657
猫巷女王i
猫巷女王i 2020-12-11 01:45

Is there any difference in performance - or otherwise - between:

ptr->a();

and

(*ptr).a(); 

?

3条回答
  •  感情败类
    2020-12-11 02:35

    [Edit]

    If the variable is defined as T* (where T is some type) then both -> and * are the same (unless ptr is null).

    If the variable is an instance of a class (by value or by reference) then -> and * should behave the same (per best practice) but this requires the class to overload them the same way.

提交回复
热议问题