0

This one class I have has only protected variable attributes like int health and int level, but no methods. Is this bad practice? I am using this for a save game function and it only needs to use variables, but requires no methods. The files look like this:

Human.h:

// Human.h - Johnny P

#pragma once

namespace SharpEngine {
    class Human {
    protected:
        std::string name;
        int level;
        int health;
        int defense;
        int strength;
        int experience;
        int money;
        int inventory[10];
    };
}

Human.cpp

// Human.cpp - Johnny P

#include <string>
#include "Human.h"
Tux
  • 1,896
  • 3
  • 19
  • 27
  • Why not use a struct for this? – Max E. Nov 07 '12 at 04:10
  • 2
    @MaxE. Why not use a class for it? –  Nov 07 '12 at 04:11
  • 2
    The acronym is POD = plain old data, and you usually use a `struct` rather than a `class`. – dmckee --- ex-moderator kitten Nov 07 '12 at 04:11
  • In C, historically, structs were used for this purpose. (In c++, structs can have functions too though.) So, sticking to that, you could use a struct here. – Navneet Nov 07 '12 at 04:12
  • @pst because there are the same thing except that you don't have to type `public:` (which you *want* because it is the only way to manipulate the contents...). – dmckee --- ex-moderator kitten Nov 07 '12 at 04:13
  • @dmckee judging by the protected, he's going to inherit from it, so `class` is a more clear term for it. I don't know if this is a valid SO question however. – Serdalis Nov 07 '12 at 04:13
  • 1
    Here is the answer: http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c – Vaibhav Desai Nov 07 '12 at 04:15
  • Can I make a `struct` have `protected` members? Do I put the struct in a header and a .cpp source? Or do I just put it in a header and include it with the .cpp file I wanted to inherit it from? – Tux Nov 07 '12 at 04:17
  • @Tux Sure. They just default to `public`. But if you *do* intend to inherit, then `class` may convey intent better than `struct`. – dmckee --- ex-moderator kitten Nov 07 '12 at 04:18
  • @dmckee Should I just leave it as a class then? – Tux Nov 07 '12 at 04:19
  • @Tux: Honestly, it doesn't matter. The only differences between a class and a struct that matter are A) structs use public inheritance by default, and B) struct members are public by default. That's it, chose whatever you like. I don't think any other maintainers are going to find themselves utterly bewildered by a class which contains only simple data members. – Ed S. Nov 07 '12 at 06:21
  • Use std::array instead old-C-style array in your class. It hasn't initialization issues (MSVS, for example, gives you a warning for that class) and provides same memory/cpu efficiency due inline interface. Requires C++11 compiler, of course. – Eugene Mamin Nov 07 '12 at 07:01

2 Answers2

2

Well, "bad practice" is a somewhat laden term.

Let's rather say that a common pattern is "Dumb Data" where the class/struct is just there to contain the data. This is not quite the same as "Plain Old Data" (POD), which is a C-compatible struct.

Having said that, using "protected" is not typical of a Dumb Data pattern. In using "protected" you imply that you will derived from it, which then implies at the very least a virtual destructor.

Keith
  • 6,756
  • 19
  • 23
0

Header-only types are fine, ditch the "implementation file" that has no implementation.

Useless files are bad practice.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720