taming-cpp

Experiments with C++ and comparisons with C
git clone https://git.tronto.net/taming-cpp
Download | Log | Files | Refs | README

all-constructors.cpp (2595B)


      1 #include <iostream>
      2 
      3 class T {
      4 public:
      5 	T(const std::string& a) : name{a} {
      6 		std::cout << "Called constructor for " << name
      7 		          << " (" << this << ")" << std::endl;
      8 	}
      9 
     10 	~T() {
     11 		std::cout << "Called destructor for " << name
     12 		          << " (" << this << ")" << std::endl;
     13 	}
     14 
     15 	T(T& other) : name{other.name} {
     16 		std::cout << "Copy constructor called for " << name
     17 		          << " (" << this << " copied from "
     18 		          << &other << ")" << std::endl;
     19 	}
     20 
     21 	T& operator=(T& other) {
     22 		name = other.name;
     23 		std::cout << "Copy assignment called for " << name
     24 		          << " (" << this << " copied from "
     25 		          << &other << ")" << std::endl;
     26 		return *this;
     27 	}
     28 
     29 	T(T&& other) : name{other.name} {
     30 		std::cout << "Move constructor called for " << name
     31 		          << " (" << this << " moved from "
     32 		          << &other << ")" << std::endl;
     33 	}
     34 
     35 	T& operator=(T&& other) {
     36 		name = other.name;
     37 		std::cout << "Move assignment called for " << name
     38 		          << " (" << this << " moved from "
     39 		          << &other << ")" << std::endl;
     40 		return *this;
     41 	}
     42 private:
     43 	std::string name;
     44 };
     45 
     46 T do_nothing_and_return(T x) {
     47 	return x;
     48 }
     49 
     50 T create(const std::string& str) {
     51 	return T(str);
     52 }
     53 
     54 int main() {
     55 	//T bad; // Compile-time error: no default constructor.
     56 
     57 	std::cout << "---------- Declaring a simple variable ----------" << std::endl;
     58 	T a("A");
     59 	std::cout << std::endl;
     60 
     61 	// Now we can see that objects are indeed scope-bound: they
     62 	// are destroyed when they go out of scope.
     63 	std::cout << "--- Declaring a variable in a temporary scope ---" << std::endl;
     64 	{
     65 		std::cout << "Entering temporary scope" << std::endl;
     66 		T b("short-lived");
     67 		std::cout << "Exiting temporary scope" << std::endl;
     68 	}
     69 	std::cout << "Out of temporary scope" << std::endl;
     70 	std::cout << std::endl;
     71 
     72 	std::cout << "------------ Constructing by copying ------------" << std::endl;
     73 	T b("B");
     74 	T c(b);
     75 	T d = b; // This is also a copy constructor, not a copy assignment
     76 	std::cout << std::endl;
     77 
     78 	// Copy assignment operators differ from copy constructors in that they
     79 	// should also clean up the resources for the copied-to object.
     80 	std::cout << "---------------- Copy assignment ----------------" << std::endl;
     81 	c = b;
     82 	std::cout << std::endl;
     83 
     84 	std::cout << "------------ Constructing by moving -------------" << std::endl;
     85 	T e(do_nothing_and_return(b));
     86 	std::cout << std::endl;
     87 
     88 	std::cout << "---------------- Move assignment ----------------" << std::endl;
     89 	e = create("E");
     90 	std::cout << std::endl;
     91 	
     92 	std::cout << "------------- Destroying everything -------------" << std::endl;
     93 	return 0;
     94 }