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:

  1. directly accessing member reads value of member while
  2. 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

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -