Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
90 views
in Technique[技术] by (71.8m points)

c++ - Initializing objects in Class

I'm trying to write a Class that defines the stats from a champion/character and I want to initialize "champions" with name, id, hp, ... in the Class My question is how do I do that? Do I need another class or do I define objects in the class which I did ?

#include <iostream>
using namespace std;


class ChampionStats {
    private:
        
    int m_id;
    string m_name;           // champion ID and Stats
    int m_hp;
    int m_ad;

    public:
        
    ChampionStats(int id, string name, int hp, int ad) {
        m_id = id;
        m_name = name;                  // initialize Champion
        m_hp = hp;
        m_ad = ad;
    }
    
    void printChamp() {                     // print champion stats 
        cout << "ID: "   << m_id  << endl
             << "Name: " << m_name << endl                      
             << "HP: "   << m_hp << endl
             << "AD: "   << m_ad << endl;
    }
};

int main() {
       
    return 0;
}
question from:https://stackoverflow.com/questions/65893383/initializing-objects-in-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's far too annoying to attempt to define ChampionStats instances inside the ChampionStats class. They would have to be made static or every instance would contain all of the pre-built champions. That includes the pre-built champions themselves, and that's a particularly nasty bit of recursion that the compiler's going to shut right down.

But if you make them static, you're back to defining the instances outside of the class.

Instead I pitch a namespace as a simple direct solution.

namespace champions
{
    ChampionStats Fred  ({1, "Fred",   42, 96);
    ChampionStats Abby  ({2, "Abby",   88, 11);
    ChampionStats Stinky({3, "Stinky", 18, 77);
}

But a container might scale a bit better.

int nextId = 0; // keep track of the next available ID
map<string, ChampionStats> champs =
{
 {"Fred",   {++nextId, "Fred",   42, 96}},
 {"Abby",   {++nextId, "Abby",   88, 11}},
 {"Stinky", {++nextId, "Stinky", 18, 77}},
};

You can keep adding rows to the list until the cows come home and nothing else needs to change.

Usage:

int main()
{
    champs.at("Fred").printChamp();
    champs.at("Abby").printChamp();
}

But it's clunky. The name is repeated twice, making it really easy to typo or cut-n-paste-n-forget. There's probably a better way to pull this off with some groovy template voodoo, but I'm no wizard. Here I'd use a macro.

int nextId = 0; // keep track of the next available ID
#define BUILD_CHAMP(name, hp, ad) {name,{++nextId, name, hp, ad}}
map<string, ChampionStats> champs =
{
 BUILD_CHAMP("Fred",   42, 96),
 BUILD_CHAMP("Abby",   88, 11),
 BUILD_CHAMP("Stinky", 18, 77),
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...