Constructor and destructor of template base class derived class __ function

Source: Internet
Author: User
//constructorofclassderivedfromtemplateclass.cpp:Defines the entry point for the console
Application.
	#include "stdafx.h" template <class t> Class A {public:a () {} A (T c) {} A (a &a) {} ~a () {
	} A &operator= (a &a) {return *this;

}
}; Template <class t> class b:public a<t> {public:b ()//default constructor will invoke the default constructor {} B (T c) of the template base class: A (c)//non-default constructor does not Calls the Non-default constructor of the template base class, {//Therefore, for a non-default constructor of the template base class. You should apply an initialization parameter list in a derived class to explicitly call the Non-default constructor of the template base class}//b (b &b): A (b)//copy constructor will overload the copy constructor of the template base class//{ So if you need a copy constructor for the template base class, you can define a copy constructor in a derived class,//}//, or you can also apply an initialization parameter list in a derived class to explicitly call the copy constructor of the template base class ~b ()//destructors will invoke the destructor of the template base class {}//b &oper
	Ator= (B &b) The overloaded assignment operator//{//overloaded assignment operators for template base classes, so if you want overloaded assignment operators for template base classes, you should not define overloaded assignment operators//return *this in a derived class;

//}
};
	int _tmain (int argc, _tchar* argv[]) {b<int> B, C;
	b = C;
	B<int> d (1);
	B<int> e (c);
return 0; }

Derived classes default constructor constructors for template base classes  non-default constructor  copy constructor  overload assignment operator
//Implicit call          Yes           Yes       no            no
//can manually call Yes No    Yes           no
//inheritance              no              no No Yes           Yes
//Overload              No no no              Yes           Yes

Remark: Derived classes are implicitly invoked on the constructor and destructor of the template base class, not inherited or overloaded. This is reflected in the invocation of both the constructor and the destructor of the derived class and the template base class when constructing and destructors.
//          derived classes do not inherit a Non-default constructor on a template base class, so there is no need to discuss overloading a non-default constructor, but a derived class can manually explicitly call a Non-default constructor on a template base class.

MEM: Derived classes on template base classes, default constructors and destructors are implicitly called
//       derived classes to the template base class, and only all constructors can explicitly call
//       derived classes on the template base class in the initialization parameter list, Copy constructors and overloaded assignment operators can be inherited and overloaded

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.