博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Effective C++: {}初始化
阅读量:6846 次
发布时间:2019-06-26

本文共 4602 字,大约阅读时间需要 15 分钟。

hot3.png

#include 
#include
/* 均是在vs2015上面测试 */class initializer {private: std::vector
vec{};public: initializer(const int& left, const int& right); initializer(const std::initializer_list
& list); ~initializer() = default;};initializer::initializer(const int& left, const int& right){ vec.push_back(left); std::cout << "use left and right" << std::endl;}initializer::initializer(const std::initializer_list
& list) :vec(list){ std::cout << "using std::initializer_list" << std::endl;}class initialized {private: std::vector
vec;public: initialized(const int& left, const int& right); //注意这里是explicit的,也就是说不能从std::initializer_list转换到(const int&, const int&, const int&). explicit initialized(const int& left, const int& mid, const int& right); ~initialized() = default;};initialized::initialized(const int& left, const int& right){ (this->vec).push_back(left); std::cout << "initialized:: left and right" << std::endl;}initialized::initialized(const int& left, const int& mid, const int& right){ (this->vec).push_back(left); std::cout << "initialized: left mid and right" << std::endl;}int main(){ //case 1: int i; int j{}; //被初始化为0. int* ptr; //未被初始化的指针. int* p{}; //被初始化为nullptr. //std::cout << i << std::endl; //在vs2015上面报错.dev C++上面OK. std::cout << j << std::endl; //输出: 0. //if (ptr == nullptr) { //error,使用了未初始化的变量ptr(在vs2015上面). // std::cout << std::boolalpha << true << std::endl; //} if (p == nullptr) { std::cout << std::boolalpha << true << std::endl; //输出: true. } //case 2: int x1(5.3); int x2 = 5.3; //int x3{ 5.3 }; //error, 不能从double转到int. //int x4 = { 5.3 }; //error, 不能从double转到int. std::cout << x1 << " " << x2 << " " /*<< x3 << " " << x4*/ << std::endl; //case 3: initializer ini1(77, 5); //initializer::initializer(const int&, const int&); initializer ini2{ 77, 5 }; //initializer::initializer(const std::initializer_list
&); initializer ini3{ 77, 5, 42 }; //同上. initializer ini4 = { 77, 5 }; //同上. //case 4: std::cout << "------------------" << std::endl; initialized ini5(77, 5); //initialized::initialized(const int&, const int&) initialized ini6{ 77, 5 }; //initialized::initialized(const int&, const int&) initialized ini7{ 77, 5, 42 }; //initialized::initialized(const int&, const int&, const int&) initialized ini8 = { 77, 5 }; //initialized::initialized(const int&, const int&) //initialized ini9 = { 77, 5, 42 }; //error. return 0;}

 

case 5:

#include
#include
class Initial {public: Initial() = default; Initial(const int& x_, const double& y_) :x(x_), y(static_cast
(y_)) { std::cout << "constructor 1" << std::endl; } Initial(const int& x_, int* ptr) :x(x_), y(x_) {} //注意这里指明了显示的构造函数. explicit Initial(std::size_t* ptr, const int& x_) :x(x_), y(x_) {} Initial(const Initial& other) :x{ other.x }, y{ other.y } { std::cout << "copy-constructor" << std::endl; } Initial(Initial&& other) :x{ std::move(other.x) }, y{ std::move(other.y) } { std::cout << "move-constructor" << std::endl; } Initial& operator=(const Initial& other) { x = other.x; y = other.y; std::cout << "operator= const&" << std::endl; return *this; } Initial& operator=(Initial&& other) { x = std::move(other.x); y = std::move(other.y); std::cout << "operator= const&" << std::endl; return *this; }private: int x; int y;};int main(){ Initial init(20, 30.5); Initial initTwo{ 20, 30.5 }; Initial initThree = { 20, 30.5 }; //constructor 1. Initial initFour(20, nullptr); Initial initFive{ 20, nullptr }; Initial initSix = { 20, nullptr }; Initial initSeven(nullptr, 20); Initial initEight{ nullptr, 20 };// Initial initNine = { nullptr, 20 }; //error; return 0;}

 

case 6-8:

#include 
#include
struct Test1{ int number;};struct Test2{ int number2{ 20 };};struct Test3{ Test3(const int& n):number3{ n }{} int number3{ 30 };};struct TestBase{ virtual ~TestBase()=default;};struct Test4 : public TestBase{ virtual ~Test4()=default; int number4; int number5;};int main(){ //case 6: Test1 t1{ 10 }; std::cout << t1.number << std::endl; //case 7:// Test2 t2{ 20 }; //error! not found this constructor. //case 8: Test3 t3{ 30 }; //ok! //case 9:// Test4 t4{ 20, 30 }; //error! return 0;}

 

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/688268

你可能感兴趣的文章