c++ - Accessing variable of class normally or by a function -
this question has answer here:
class abc{ public : int a; public : int getdata(){ return a; } } void main() { abc abc; cout<< abc.a; //1 cout<<abc.getdata();//2 }
now if i'm accessing variable "a"
in case compiler take less time access 'a'
. guess first case not sure.
the 2 methods not strictly equivalent:
- directly accessing member reads value of member while
- the method returns copy of variable, read copy such.
with regards performance ofcourse #1 should faster since no copy involved, modern compilers apply copy elision remove copy being created.
rather performance should consider uniformity of coding guidelines followed in organization/institute.
Comments
Post a Comment