Pass parameter to a get method, can it generate an error in C++?

Asked

Viewed 31 times

1

I’m starting to study POO now and it’s a bit weird for me. But I’d like to know if I pass some parameter in a method get, can result in a mistake? Ex:

class MostraNum
{
      private:
      int x;
      public:
      int getRetornaNum(int x)
      {
          return x;
      }
};

int main()
{
     MostraNum result;
     cout<<result.getRetornaNum(100)<<endl;
     return 0;
}
  • Be more specific. What you expect the program to do?

1 answer

1

Not,

Get is just a name used by convention and means nothing.

Your function will return the parameter passed and not the member variable 'x' because c++ prioritizes local variables when the name is the same, if you wanted to return the member variable x you should explicitly do:

return MostraNum::x

But for organization reasons and to make it easier to read your code give a special name to your variable member and do not use repeated names, such as in hungarian notation would m_nX.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.