Re: [Newbie] How to use a class in C++
pozz <pozzugno@gmail.com> wrote:
In C I can create settings.h with the interface:
---
int settings_read(const char *name, char *value, size_t valuesize);
int settings_save(const char *name, const char *value);
---
If that design suffices for your purposes, then I don't see why you
couldn't use that exact same code in your C++ program as well. (Although
if this is a generic library to be used in more than one project, you
might want to consider putting those functions inside a namespace with
some appropriate name. Not that it's mandatory, but it lessens the chances
of name collisions in large projects.)
One of the practical advantages of classes is that, like structs, you
can instantiate more than one at a time, and each instance will be
independent of each other, and they can co-exist at the same time.
(The advantage over C structs is that you have more control over access
rights, it's modular because you can use member functions, and you have
RAII, which is very useful in many cases. This not even going into
inheritance and dynamic binding.)
However, just because there are classes doesn't necessarily mean that
everything *must* be a class. If someting can be implemented as a
function, and there's no need for anything else, then why not.