Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- class Type {
- public:
- Type(int v) : value(v) {
- std::cout << "Type(): " << value << std::endl;
- pointer = new int(10);
- }
- Type(const std::string& a, const std::vector<int>& b)
- : name(a)
- , data(b)
- {}
- Type(const Type& other)
- : name(other.name)
- , data(other.data)
- {}
- Type(Type&& other)
- : name(std::move(other.name))
- , data(std::move(other.data))
- {}
- ~Type() {
- std::cout << "~Type()" << value << std::endl;
- delete pointer;
- }
- Type& operator= (const Type& other) {
- std::cout << "operator=(const Type&) " << value << std::endl;
- name = other.name;
- data = other.data;
- return *this;
- }
- Type& operator= (Type&& other) {
- std::cout << "operator=(Type&&): " << value << std::endl;
- name = std::move(other.name);
- data = std::move(other.data);
- return *this;
- }
- public:
- std::string name;
- std::vector<int> data;
- int value = -1;
- int* pointer = nullptr;
- };
- Type func() {
- Type p(1);
- p.name = "12wefsdfsdfsefesfwefwefefewsfsdfsdf";
- p.data = {123,122,2222,2222};
- return p;
- }
- class Rational {
- public:
- Rational(int p, int q) {
- if (q == 0) {
- throw std::invalid_argument("q must be not zero");
- }
- p_ = p;
- q_ = q;
- }
- bool set_q(int q) {
- if (q == 0) {
- return false;
- }
- q_ = q;
- }
- private:
- int p_;
- int q_;
- };
- void f() {
- try {
- Rational a(10,0);
- std::cout << "im here" << std::endl;
- } catch(std::runtime_error& e) {
- std::cout << "something wrong: " << e.what() << std::endl;
- }
- }
- int main() {
- try {
- f();
- } catch(...) {
- std::cout << "any exception" << std::endl;
- }
- std::cout << "agter init" << std::endl;
- int value;
- std::cin >> value;
- if (value < 1000) {
- throw std::invalid_argument("input value less then 1000");
- }
- std::cout << "ok" << std::endl;
- Type* pt = nullptr;
- std::cout << pt << std::endl;
- std::cout << pt->name << std::endl;
- std::cout << "done" << std::endl;
- pt = new Type("sdfsdfsdf", {123, 443, 34333});
- int *a = new int;
- int* ptr = new int(10);
- std::cout << *a << std::endl;
- delete a;
- a = nullptr;
- delete ptr;
- delete pt;
- delete a; // delete nullptr; valid
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement