search

Home  >  Q&A  >  body text

C++内部类有关构造函数的问题

这是我定义的士兵类

class Warrior {
private :
    string name;//士兵的名字
    int id;//编号
    int strength;//生命值
    int attack;//攻击力
public:
    Warrior() { cout << "new";}
    Warrior (string name,int id,int strength,int attack=0):name(name),id(id),strength(strength),attack(attack){
        cout <<name<< id << strength << attack << endl;}
};

这是我定义的总部类

class Headquarter {
private:
    string type;//总部类别
    Warrior wa;
    int blue_order[5] = { 1,2,3,4,5 };//蓝军造士兵的顺序
    int red_order[5] = { 4,1,5,2,3 };//红军造士兵的顺序
    int hp_list[5] = {0};//按蓝军顺序为每种士兵赋予的生命值
    int hp;
     int iceman_num=0, lion_num=0, wolf_num=0, ninja_num=0, dragon_num=0;
public:
    Headquarter(string type, int hp) :type(type), hp(hp) { }
    Headquarter(string type,string name,int id,int strength,int attack,int hp):type(type),wa(name,id,strength,attack),hp(hp){}
    void setHp(int a[]);//为每个士兵赋生命初始值
    void creatWarrior(int i);//通过总部造士兵
};

我想通过每个总部来造属于自己的士兵,所以把Warrior定义成Headquarter的内部类。
我有以下几个问题:
1.在我初始化Headquarters类时,是不是Warrior类也会初始化?但是我在创造总部的时候不想有士兵,我应该怎么办?
2.下面是我在Headquarter类中写的创造士兵的方法

 void Headquarter::creatWarrior(int i)
{
    switch (i)
    {
    case 1:Warrior  dragon(); dragon_num++; break;
    case 2:Warrior  ninja(); ninja_num++; break;
    case 3:Warrior iceman(); iceman_num++; break;
    case 4:Warrior lion(); lion_num++; break;
    case 5:Warrior wolf(); wolf_num++; break;
    default:
        break;
    }
}
比如这一句`
case 1:Warrior  dragon(); dragon_num++; break;`
如果写成
`case 1:Warrior  dragon; dragon_num++; break;`(少了括号)
编译器就会报错。这样不能实例化一个对象吗?还有上一句实例化dragon对象时用的是什么构造函数,似乎并没有调用无参构造函数啊。
才接触C++有些问题表达的不清楚。。。见谅哈。

黄舟黄舟2774 days ago392

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 13:42:14

    Just give a brief answer.

    The way the question is written now is not to define Warrior as an internal class of Headquarter, but to define a Headquarter member within the Warrior class.

    1. When I initialize the Headquarters class, will the Warrior class also be initialized? But I don't want to have soldiers when creating the headquarters, what should I do?

    You can use std::vector<Warrior> warriors; to create a dynamic array, so that when creating an object of the Headquarters class, only an empty std::vector will be initialized, and then pass the creatWarrior when calling the warriors.push_back(Warrior(some-parameters...)); function way to create soldiers.

    2. Here is the method I wrote in the Headquarter class to create soldiers...

    There seems to be something wrong with your creatWarrior function. Local variables are created here and will be destroyed after the function ends.

    One of the most basic methods is to achieve it through polymorphism, that is,

    • Implement Warrior as a base class

    • Implement specific soldier types as subclasses, such as Dragon classes, etc.

    • is dynamically bound through pointers, that is, in the Headquarter class, use std::vector<std::shared_ptr<Warrior>> warriors; as a member to store the pointer of the soldier, and in the creatWarrior function, use the case 1: warriors.push_back(new Dragon(some-parameters...)); dragon_num++; break; method to create the soldier.

    reply
    0
  • Cancelreply