C + + Telescoping constructor is not supported until c++11

Source: Internet
Author: User

Telescoping Constructor:see effective Java 2nd Edition Item 2

If you want to use telescoping constructor but your compiler does not support C++11 standards, you can use default par Ameter instead, see Code Snippets ' main.cpp ', ' Box.h ', ' Box.cpp '.

If your compiler supports C++11 standards, the syntax of telescoping are shown in code snippet ' Box2.h ' and ' Box2.cpp ', and You might need to use '-std=c++11 ' parameter if you're compiling your source code.

For-now, I-d like-to-use default parameter.

Main.cpp

1#include"Box.h"2 3 intMain ()4 {5 box box;6 box.print ();7Box Box2 (1);8 box2.print ();9Box Box3 (1,'A');Ten box3.print (); OneBox Box4 (1,'A',1.88); A box4.print (); -     return 0; -}

Box.h

1 #ifndef Box_h2 #defineBox_h3 4 classBox5 {6      Public:7Box (Const int&_a =0,Const Char&_b ='M',Const Double&_c =0.99);8~Box ();9         voidprint ();Ten  One     Private: A         intA; -         Charb; -         DoubleC; the }; -  - #endif //Box_h

Box.cpp

1#include"Box.h"2 3#include <iostream>4 5 using namespacestd;6 7Box::box (Const int&_a,Const Char&_b,Const Double&_c)8 : A (_a), B (_b), C (_c)9 {Ten  One } A  -box::~Box () - { the  - } -  - voidBox::p rint () { +cout <<"A ="<< a <<", B ="<< b <<", C ="<< C <<Endl; -}

Box2.h

1 #ifndef Box_h2 #defineBox_h3 4 classBox5 {6      Public:7Box (Const int&_a,Const Char&_b,Const Double&_c);8Box (Const int&_a,Const Char&_b);9Box (Const int&_a);Ten Box (); One~Box (); A         voidprint (); -  -     Private: the         intA; -         Charb; -         DoubleC; - }; +  - #endif //Box_h

Box2.cpp

1#include"Box.h"2 3#include <iostream>4 5 using namespacestd;6 7Box::box (Const int&_a,Const Char&_b,Const Double&_c)8 : A (_a), B (_b), C (_c)9 {Ten  One } A  -Box::box (Const int&_a,Const Char&_b) -: Box (_a, _b,0.99) the { -  - } -  +Box::box (Const int&_a) -: Box (_a,'M') + { A  at } -  - Box::box () -: Box (0) - { -  in } -  tobox::~Box () + { -  the } *  $ voidBox::p rint () {Panax Notoginsengcout <<"A ="<< a <<", B ="<< b <<", C ="<< C <<Endl; -}

C + + Telescoping constructor is not supported until c++11

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.