When should I use classes in C++?

A simple answer that's more in line with what C++ is about is "when there is an invariant"

A class invariant is a statement (a boolean function if you will) about the contents of an object, which is always true at the end of every function that operates on the object. (If it cannot be satisfied, the function does not return, and throws an exception instead)

For example, a typical std::vector with an empty allocator has three members: start pointer, end pointer, end-of-capacity pointer. At the end of any constructor, as well as at the end of any member or non-member function that works on a vector, it's always true that capacity is greater or equal end which is greater or equal begin, among other things.

It would be possible to use an array of three pointers instead, but it would be wrong because arrays (and other aggregate types) do not enforce invariants. They can be created with any three pointer values.

After this you can talk about encapsulation, inheritance, behavior vs. data, the SOLID design rules, etc, but the core idea of classes in C++ is enforcement of invariants. That's why it differs from all other languages with regards to virtual dispatch during construction.
Written Nov 10, 2014 • View Upvotes

Answer by Sergey Zubkov:

A simple answer that's more in line with what C++ is about is "when there is an invariant"

A class invariant is a statement (a boolean function if you will) about the contents of an object, which is always true at the end of every function that operates on the object. (If it cannot be satisfied, the function does not return, and throws an exception instead)

For example, a typical std::vector with an empty allocator has three members: start pointer, end pointer, end-of-capacity pointer. At the end of any constructor, as well as at the end of any member or non-member function that works on a vector, it's always true that capacity is greater or equal end which is greater or equal begin, among other things.

It would be possible to use an array of three pointers instead, but it would be wrong because arrays (and other aggregate types) do not enforce invariants. They can be created with any three pointer values.

After this you can talk about encapsulation, inheritance, behavior vs. data, the SOLID design rules, etc, but the core idea of classes in C++ is enforcement of invariants. That's why it differs from all other languages with regards to virtual dispatch during construction.

When should I use classes in C++?

Advertisements

Leave a comment

Filed under Life

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s