Skip to main content
There are no child objects!
Source Link
Toby Speight
  • 88.3k
  • 14
  • 104
  • 326

Is this an effective implementation of a parent class Class that contains a "global" tracker on it's children?counts its instances

I'd love some feedback on the C++ base class shown below. It's basically a toy problem for a fake video game where the monsters that you have to avoid will all be classes that are children ofsub-classed from the class shown below.

I'm particularly interested in feedback on the way I handled the character_count counter, which is meant to count the total number of monsters created (for simplicity, I'm assuming there is no need to ever decrement the counter): I'm practicing "Avoid initialization order problems across translation units by replacing non-local static objects with local static objects.practising "Avoid initialization order problems across translation units by replacing non-local static objects with local static objects." From Item 4 of Effective C++Effective C++ by Scott Meyers (if you have the book and are going to look, the bit I quoted is the summary he provides for the last third of Item 4... there are two other points he makes earlier in the item).

Character.h

Character.h

#pragma once

enum CharacterType { kCharacterBaseClass, kGhost, kGoblin, kVampire };

struct CharCount {
  int character_count = 0;
};

class Character {
 public:
  explicit Character(CharacterType type = kCharacterBaseClass);
  static CharCount &CharacterCount();
  [[nodiscard]] CharacterType GetType() const;
  virtual void Animate() const;


 protected:
  CharacterType type_;
};

Character.cpp

Character.cpp

#include "GmChrctrs/Character.h"

#include <iostream>

Character::Character(CharacterType type) : type_(type) {
  ++Character::CharacterCount().character_count;
}

CharCount &Character::CharacterCount() {
  static CharCount char_count;
  return char_count;
};

CharacterType Character::GetType() const { return type_; }

void Character::Animate() const {
  std::cout << "generic character is animated" << std::endl;
}

Is this an effective implementation of a parent class that contains a "global" tracker on it's children?

I'd love some feedback on the C++ base class shown below. It's basically a toy problem for a fake video game where the monsters that you have to avoid will all be classes that are children of the class shown below.

I'm particularly interested in feedback on the way I handled the character_count counter, which is meant to count the total number of monsters created (for simplicity, I'm assuming there is no need to ever decrement the counter): I'm practicing "Avoid initialization order problems across translation units by replacing non-local static objects with local static objects." From Item 4 of Effective C++ by Scott Meyers (if you have the book and are going to look, the bit I quoted is the summary he provides for the last third of Item 4... there are two other points he makes earlier in the item).

Character.h

#pragma once

enum CharacterType { kCharacterBaseClass, kGhost, kGoblin, kVampire };

struct CharCount {
  int character_count = 0;
};

class Character {
 public:
  explicit Character(CharacterType type = kCharacterBaseClass);
  static CharCount &CharacterCount();
  [[nodiscard]] CharacterType GetType() const;
  virtual void Animate() const;


 protected:
  CharacterType type_;
};

Character.cpp

#include "GmChrctrs/Character.h"

#include <iostream>

Character::Character(CharacterType type) : type_(type) {
  ++Character::CharacterCount().character_count;
}

CharCount &Character::CharacterCount() {
  static CharCount char_count;
  return char_count;
};

CharacterType Character::GetType() const { return type_; }

void Character::Animate() const {
  std::cout << "generic character is animated" << std::endl;
}

Class that counts its instances

I'd love some feedback on the C++ base class shown below. It's basically a toy problem for a fake video game where the monsters that you have to avoid will all be sub-classed from the class shown below.

I'm particularly interested in feedback on the way I handled the character_count counter, which is meant to count the total number of monsters created (for simplicity, I'm assuming there is no need to ever decrement the counter): I'm practising "Avoid initialization order problems across translation units by replacing non-local static objects with local static objects." From Item 4 of Effective C++ by Scott Meyers (if you have the book and are going to look, the bit I quoted is the summary he provides for the last third of Item 4... there are two other points he makes earlier in the item).

Character.h

#pragma once

enum CharacterType { kCharacterBaseClass, kGhost, kGoblin, kVampire };

struct CharCount {
  int character_count = 0;
};

class Character {
 public:
  explicit Character(CharacterType type = kCharacterBaseClass);
  static CharCount &CharacterCount();
  [[nodiscard]] CharacterType GetType() const;
  virtual void Animate() const;


 protected:
  CharacterType type_;
};

Character.cpp

#include "GmChrctrs/Character.h"

#include <iostream>

Character::Character(CharacterType type) : type_(type) {
  ++Character::CharacterCount().character_count;
}

CharCount &Character::CharacterCount() {
  static CharCount char_count;
  return char_count;
};

CharacterType Character::GetType() const { return type_; }

void Character::Animate() const {
  std::cout << "generic character is animated" << std::endl;
}
Source Link

Is this an effective implementation of a parent class that contains a "global" tracker on it's children?

I'd love some feedback on the C++ base class shown below. It's basically a toy problem for a fake video game where the monsters that you have to avoid will all be classes that are children of the class shown below.

I'm particularly interested in feedback on the way I handled the character_count counter, which is meant to count the total number of monsters created (for simplicity, I'm assuming there is no need to ever decrement the counter): I'm practicing "Avoid initialization order problems across translation units by replacing non-local static objects with local static objects." From Item 4 of Effective C++ by Scott Meyers (if you have the book and are going to look, the bit I quoted is the summary he provides for the last third of Item 4... there are two other points he makes earlier in the item).

Character.h

#pragma once

enum CharacterType { kCharacterBaseClass, kGhost, kGoblin, kVampire };

struct CharCount {
  int character_count = 0;
};

class Character {
 public:
  explicit Character(CharacterType type = kCharacterBaseClass);
  static CharCount &CharacterCount();
  [[nodiscard]] CharacterType GetType() const;
  virtual void Animate() const;


 protected:
  CharacterType type_;
};

Character.cpp

#include "GmChrctrs/Character.h"

#include <iostream>

Character::Character(CharacterType type) : type_(type) {
  ++Character::CharacterCount().character_count;
}

CharCount &Character::CharacterCount() {
  static CharCount char_count;
  return char_count;
};

CharacterType Character::GetType() const { return type_; }

void Character::Animate() const {
  std::cout << "generic character is animated" << std::endl;
}