C++ Class Methods
Introduction to Class Methods
In C++, a method (member function) is a function declared within a class that operates on its objects. Member functions can access the class's private/protected data and define how objects behave.
A method can be defined directly inside the class body or declared inside the class and defined outside using the scope resolution operator (::).
Defining Methods Inside the Class
If a method is defined inside the class, it is implicitly declared with the `inline` specifier. This enables placing the definition in headers without violating the One Definition Rule. Note that `inline` does not force inlining; the optimizer decides whether to inline the call.
#include <iostream>
using namespace std;
class MyClass {
public:
void greet() {
cout << "Hello from inside the class!" << '\n';
}
};
int main() {
MyClass obj;
obj.greet();
return 0;
}
Hello from inside the class!
Defining Methods Outside the Class
For better organization, methods are often declared inside the class and defined outside using the scope resolution operator `::`. The out-of-class definition must repeat the exact signature (including qualifiers like `const`, `noexcept`, reference qualifiers, etc.).
#include <iostream>
using namespace std;
class MyClass {
public:
void greet(); // Declaration only
};
// Definition outside the class
void MyClass::greet() {
cout << "Hello from outside the class!" << '\n';
}
int main() {
MyClass obj;
obj.greet();
return 0;
}
Hello from outside the class!
Using Parameters and Return Values
Member functions can take parameters and return values like free functions. Use `const` on methods that do not modify the object's state.
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) const { // const: does not modify the object
return a + b;
}
};
int main() {
Calculator calc;
cout << "Sum: " << calc.add(5, 3);
return 0;
}
Sum: 8
Const Methods and the this Pointer
Inside a `const` member function, the implicit `this` pointer is `const ClassName*`, so you cannot modify non-`mutable` data members. You can provide both const and non-const overloads when appropriate.
#include <iostream>
#include <string>
using namespace std;
class Buffer {
string data {"hi"};
public:
const string& get() const { return data; } // callable on const objects
string& get() { return data; } // callable on non-const objects
};
int main() {
Buffer b;
cout << b.get() << '\n';
b.get() += " there"; // uses non-const overload
cout << b.get() << '\n';
return 0;
}
hi hi there
Static Methods
Static member functions do not have a `this` pointer and cannot access non-static members unless given an object/reference. They are often used for utilities related to the class.
#include <iostream>
using namespace std;
class Counter {
static int s_live;
public:
Counter() { ++s_live; }
~Counter() { --s_live; }
static int live() { return s_live; } // no 'this' here
};
int Counter::s_live = 0;
int main() {
Counter a, b;
cout << "Objects alive: " << Counter::live();
return 0;
}
Objects alive: 2
Best Practices
- Keep each method focused on a single task for clarity.
- Mark methods `const` when they do not modify the object's observable state.
- Define short/simple methods inside the class (implicitly `inline`), but move complex logic to out-of-class definitions.
- Keep signatures consistent (match qualifiers in declarations/definitions).
- Group related methods and consider `static` for utilities that do not depend on object state.