变量有什么问题international_standard_book_number
?我怎样才能让它改变,无论何时isbn_field_i
改变?
#include <iostream>
#include <string>
class ISBN
{
private:
unsigned int isbn_field_1 = 0;
unsigned int isbn_field_2 = 0;
unsigned int isbn_field_3 = 0;
char digit_or_letter = 'a';
std::string international_standard_book_number =
std::to_string(isbn_field_1) + "-" + std::to_string(isbn_field_2) + "-" +
std::to_string(isbn_field_3) + "-" + digit_or_letter;
public:
ISBN()
{
isbn_field_1 = 0, isbn_field_2 = 0, isbn_field_3 = 0, digit_or_letter = 'a';
}
ISBN(unsigned int a, unsigned int b, unsigned int c, char d)
{
isbn_field_1 = a, isbn_field_2 = b, isbn_field_3 = c, digit_or_letter = d;
}
friend std::ostream& operator<<(std::ostream& os, ISBN const& i)
{
return os << i.international_standard_book_number;
}
};
int
main()
{
ISBN test(1, 2, 3, 'b');
std::cout << test << "\n";
return 0;
}
输出:
0-0-0-a
期望的输出:
1-2-3-b
编辑:这个问题的目的不是我的(为什么,而不是如何),它的答案对我的帮助不如这个主题的答案。