C + + Learning constructor, copy constructor

Source: Internet
Author: User


Extended question one: whether the parameters in the copy constructor are const to the effect of the copy constructor.

Most people on the web can only interpret the problem as "when you don't want to make changes to the parameters, you need to add the Const keyword", but not carefully differentiate between the two situations. Take the following procedure as an example:

Dog.h

#ifndef __test_header__dog__#define __test_header__dog__#include <stdio.h>class Dog{public:    Dog ();    Dog (dog &dog);}; #endif
Dog.cpp
#include "Dog.h" #include <iostream>using namespace Std;dog::D og () {    cout<< "Dog ()" <<ENDL;};D OG::D og (dog &dog) {    cout<< "dog &dog" <<endl;};
Main.cpp

#include <iostream> #include <string> #include "Dog.h" using namespace Std;int main (int argc, const char * argv[ ]) {    //Insert code    here ... Dog Dog1;    Dog dog2 = dog1;    return 0;}
after the run, the output results are:


If you change the dog dog1 to a const dog DOG1, you will get an error when compiling again.


The hint does not match the constructor, because we do not define a parameter that is a const DOG &dog copy constructor.

So what happens if we change the program slightly and change it to the following code?

Dog.h

#ifndef __test_header__dog__#define __test_header__dog__#include <stdio.h>class Dog{public:    Dog ();    Dog (dog &dog);    Dog (const dog &dog);    dog& operator= (Dog &dog);}; #endif
Dog.cpp

#include "Dog.h" #include <iostream>using namespace Std;dog::D og () {    cout<< "Dog ()" <<ENDL;};D OG::D og (const dog &dog) {    cout<< "dog (const dog &dog)" <<endl;};

Main.cpp

#include <iostream> #include <string> #include "Dog.h" using namespace Std;int main (int argc, const char * argv[ ]) {    //Insert code    here ... Dog Dog1;    Dog dog2 = dog1;    return 0;}

Will it happen because there is no copy constructor without const and a compile error occurs? The answer is no, because we provide a copy constructor with the const keyword, so either the const dog DOG1 or the Dog dog1 can use a copy constructor with the const keyword, Because a copy constructor with Const is more stringent than a copy constructor without a const, the parameters he can accept can be const constants or variables without const, which is a bit backwards compatible. So in this case, if the copy constructor without const is found, the copy constructor with Const is called.

Here you may ask, what happens if you provide 2 copy constructors with const and no const at the same time? Modify the Main.cpp as follows

#include <iostream> #include <string> #include "Dog.h" using namespace Std;int main (int argc, const char * argv[ ]) {    //Insert code    here ... Dog Dog1;    Const DOG DOG2;    Dog dog3 = dog1;    Dog dog4 = dog2;    return 0;}

The answer is:

The copy constructor without const is called by the Dog Dog1 as the operand to the right of the equals sign.

The const DOG DOG1 the copy constructor with Const when the operand to the right of the equals sign is called.


Let's talk about the default copy constructor provided by the system, the default copy constructor of the system I guess there are two possibilities:

First: Only copy constructors with Const are available, because they can be compatible with parameters without const

The second: Depending on the case, if the parameter with const constructs a copy constructor with const, if the parameter is not const, constructs a non-const function

But it doesn't make much sense to dwell on it.

C + + Learning constructor, copy constructor

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.