简体   繁体   English

对c ++类的怀疑

[英]Doubts about c++ classes

I'm doing a simple c++ project about a card game and i'm using the following class to represent cards: 我正在做一个关于纸牌游戏的简单c ++项目,我正在使用以下类来表示卡片:

class card{
public:
    int suit;
    int value;
    bool check;//i need it for various things
    card(): suit(0), value(0), check(false) {}
    void set_check(bool a){check=a;}
    void card_name(); //given a card name, it generate the suit and value of the card
    string suit_name(int); //print card suit name
    string value_name(int);//print card value name
};

Now, it's the first time i use classes so i have some doubts: 现在,这是我第一次使用课程,所以我有些疑惑:

1) The first time i used the class card it didn't have any default constructor, it was like this: 1)我第一次使用类card它没有任何默认构造函数,它是这样的:

class card{
public:
    int suit;
    int value;
    void card_name();
    string suit_name(int);
    string value_name(int);
};

whenever i needed to initialize a card i did something like card a={0,0}; 无论什么时候我需要初始化一张卡片,我都会做一些card a={0,0}; . With the new class the constructor initialize the object, but in some functions i use the syntax card a={suit,value}; 使用新类构造函数初始化对象,但在某些函数中我使用语法card a={suit,value}; to assign numbers to members suit and value . 为成员分配数字suitvalue It worked before, but with the constructor it shows the error error: no match for 'operator=' (operand types are 'card' and '<brace-enclosed initializer list>') or also error: could not convert '{0, 0}' from '<brace-enclosed initializer list>' to 'card' 它之前有用,但是使用构造函数它会显示错误error: no match for 'operator=' (operand types are 'card' and '<brace-enclosed initializer list>')或者error: could not convert '{0, 0}' from '<brace-enclosed initializer list>' to 'card'

I know how to avoid this (i'll add a set_values member function) but i was just curious about what exactly make this happen(is it because {...} is an initializer and causes a double initialization?). 我知道如何避免这种情况(我将添加一个set_values成员函数),但我只是想知道究竟是什么导致这种情况发生(是因为{...}是一个初始化器并导致双重初始化?)。

2) Is there a different way, beside using a constructor, to set a member of a class to a default value (ie i need check to be initialized as false , unless i explicitly change it through card::set_check(bool) )? 2)除了使用构造函数之外,是否有不同的方法将类的成员设置为默认值(即我需要check以初始化为false ,除非我通过card::set_check(bool)明确地更改它)? If i defined the class like this 如果我这样定义了这个类

class card{
public:
    int suit;
    int value; 
    bool check=false;
    void set_check(bool a){check=a;}
    void card_name();
    string suit_name(int);
    string value_name(int);
};

would it work? 会有用吗? And, assuming the class as above, could i initialize an object simply by card a={0,0} without specifying check value? 并且,假设上面的类,我可以简单地通过card a={0,0}初始化一个对象而不指定check值吗?

3) Last and also the least important, so don't mind that much. 3)最后也是最不重要的,所以不要太在意。 I was wondering if there's a way to initialize and assign values to elements in an array of card without using a specific constructor for arrays. 我想知道是否有一种方法可以初始化并为card数组中的元素赋值,而无需使用特定的数组构造函数。 I mean, assume class is 我的意思是,假设上课是

class card{
    int suit;
    int value;
    bool check;
    card(int a, int b, bool c=false): suit(a), value(b), check(c) {}
   //other functions
};

When i was using the class card without constructor, i declared the following array card array[5]={{1,1},{2,3},{3,5}, ...} , but it gives me problem with the new constructor for point 1 above. 当我使用没有构造函数的类card ,我声明了以下数组card array[5]={{1,1},{2,3},{3,5}, ...} ,但它给了我问题使用上面第1点的新构造函数。

I saw on another forum that it was allowed to do something like this 我在另一个论坛上看到它被允许做这样的事情

card array[5]={card(1,1), card(2,3), card(3,5), ...}

ie initializing an array with brace-enclosed initializer and calling the constructor inside the braces. 即使用大括号括起初始化器初始化数组并调用大括号内的构造函数。 Is this really allowed? 这真的允许吗? Is there another way to do something like this using constructors? 有没有其他方法可以使用构造函数执行此类操作?

Re: 1 回复:1

card a={suit,value};

doesn't work since you have an explicitly defined default constructor. 因为您有一个显式定义的默认构造函数,所以不起作用。 You can use another constructor to make it work. 您可以使用其他构造函数使其工作。

card(int s, int v): suit(s), value(v), check(false) {}

Re: 2 回复:2

Yes, 是,

bool check=false;

is a good way to initialize check to false . 是一种将check初始化为false的好方法。

Re: 3 回复:3

If you define the second constructor, you should be able to use: 如果定义第二个构造函数,则应该能够使用:

card array[5]={{1,1},{2,3},{3,5}, ...};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM