Reputation: 160
I'm doing a simple c++ project about a card game and i'm using the following class to represent cards:
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:
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};
. With the new class the constructor initialize the object, but in some functions i use the syntax card a={suit,value};
to assign numbers to members suit
and value
. 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’
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?).
2) Is there a different way, beside using a constructor, to set a member of a class to a default value (i.e. i need check
to be initialized as false
, unless i explicitly change it through 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?
3) Last and also the least important, so don't mind that much. 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. 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.
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), ...}
i.e. 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?
Upvotes: 1
Views: 105
Reputation: 206567
Re: 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
Yes,
bool check=false;
is a good way to initialize check
to false
.
Re: 3
If you define the second constructor, you should be able to use:
card array[5]={{1,1},{2,3},{3,5}, ...};
Upvotes: 3