Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualKing Mir

Posted 22 June 2013 - 09:31 PM

Thanks, I didn't know that.

So to be sure, this would be correct:

class MyClass

{
public:
   int mAge;

   void DoSometingWithNoneMembers(const float p1, float *p2) const;
   void UpdateAge(const int &pAge);
}

void MyClass::DoSometingWithNoneMembers(const float p1, float *p2)

{
   p2 = p1;
}

void MyClass:UpdateAge(const int &pAge)
{
   mAge = *pAge;
}

Where I assume that the 'const' keyword for member functions is only needed in the prototype.

Is this correct, or does is also have to be in the implementation? (sounds logical for readability if it's also there)

Sorta. UpdateAge is properly non const. But DoSometingWithNoneMembers shouldn't be a method at all, since it doesn't do anything with the object. A more typical example of a const method would be something that returns a value based on the members. For example:

int MyClass::getDogYears() const
{
  return mAge*7;
}

#1King Mir

Posted 22 June 2013 - 09:30 PM

Thanks, I didn't know that.

So to be sure, this would be correct:

class MyClass

{
public:
   int mAge;

   void DoSometingWithNoneMembers(const float p1, float *p2) const;
   void UpdateAge(const int &pAge);
}

void MyClass::DoSometingWithNoneMembers(const float p1, float *p2)

{
   p2 = p1;
}

void MyClass:UpdateAge(const int &pAge)
{
   mAge = *pAge;
}

Where I assume that the 'const' keyword for member functions is only needed in the prototype.

Is this correct, or does is also have to be in the implementation? (sounds logical for readability if it's also there)

Sorta. UpdateAge is properly non const. But DoSometingWithNoneMembers shouldn't be a method at all, since it doesn't do anything with the object. A more typical example of a const method would be something that returns a value based on the members. For example:

 

int MyClass::getDogYears()
{
  return mAge*7;
}

PARTNERS