Компилятор: G ++ (Debian 10.2.1-6) 10.2.1 20210110 < /p>
У меня есть класс big_int, и он обрабатывает все вещи. У меня также есть перегрузки оператора, которые чисто обрабатывают арифметику и назначения.
Код: Выделить всё
class big_int {
// operator overloads
// Constructors
big_int (){}
big_int (const big_int& other){ *this = other;} // has the =overload for direct copying
explicit big_int (const char* s){ value = s;}
big_int (const std::string& s){ value = s;}
}
< /code>
int main(){
big_int num2 = "99"; // doesn't work
}
``
> error:conversion from ‘const char [3]’ to non-scalar type ‘big_int’ requested big_int num2 = "99";
Even tough this works
```cpp
int main(){
big_int str_num("45569");
}
< /code>
У меня есть следующие перегрузки оператора (я не думаю, что эта информация необходима, но все же): < /p>
class big_int {
public:
// Member Operator Overloads -> ->
// assingment overloads
// std::string
big_int& operator=(const std::string& other) { // = overload (strings)
this->value = other; // no self assingment
return *this; // de-referenced object
}
// big_int
big_int& operator=(const big_int& other) { // = overload (big_int)
if (this != &other) this->value = other.value; // no self assingment
return *this; // de-referenced object
}
// --------------------------
// plus overloads
// std::string
big_int operator+(const std::string& other) const{ // + overload (strings)
return big_int(arb_add(value, other));
}
// big_int
big_int operator+(const big_int& other) const{ // + overload
return big_int(arb_add(value, other.value));
}
// -----------------------
// minus overloads
// std::string
big_int operator-(const std::string& other) const{ // - overload
return big_int(arb_sub(value, other));
}
// big_int
big_int operator-(const big_int& other) const{ // - overload
return big_int(arb_sub(value, other.value));
}
// -------------------------------
}
// GENERAL OPERATOR OVERLOADS
std::ostream& operator
int main() {
big_int str_num("45569");
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... bject-even
Мобильная версия