This pointer in the constructor

Source: Internet
Author: User

This pointer in the constructor
Author: Wen Yu

Note: Thanks to Marshall Cline, author of the C ++ FAQ lite, and Shen Yu, translator. Thanks to Gregory Satir, author of C ++ language core, Doug Brown, and translator Zhang Mingze.

------------------------------- Principle -------------------------------

Some people think that this pointer should not be used in the constructor, because this object is not fully formed yet.

However, if you are careful, you can use the this pointer in the constructor:

● In the function body

● Initializing the list

Because "the object has not been fully formed" does not mean "nothing ".

Before entering the constructor (and its chaining), compiler will:

● Allocate memory to the class instance

● Set up the information required by the system at runtime (such as vtbl)

●## Default ## construct all Class Members

----------------------------- [Energy ]---------------------------------

The function body of the constructor (or the function called by the constructor) can be accessed reliably:

● Data members declared in the base class

● Data member of the class declaration to which the constructor belongs

This is because all the data members are guaranteed to have been fully established when the constructor starts execution.

----------------------------- [No ]---------------------------------

The constructor's function body (or the function called by the constructor) [cannot] Call down:

● The virtual function redefined by the derived class

This is because during the execution of the base class constructor, "the object is not an object of A derived class ".

--------------------------- [Sometimes ]-----------------------------------

The following are [sometimes] feasible:

● Passing any data member of this object to the initialization program of another data member

Make sure that the data member has been initialized. The good news is that you can use notable language rules that do not depend on the compiler you are using to determine whether or not the data member has been initialized. Bad news is that you must know these language rules (for example, the base class sub-objects are initialized first (if there are multiple and/or virtual inheritance, query this order !), The data members defined in the class are initialized according to the declared order in the class ). If you do not know these rules, do not pass any data members from this object (whether or not this keyword is explicitly used) to the initialization program of any other data member! If you know these rules, be careful.

---------------------------- Purpose ----------------------------------

A good OO design emphasizes "high aggregation", which will produce many objects with a single small responsibility (in fact, the "single Responsibility Principle" is the most basic oo principle ).

Therefore, the collaboration between small objects needs to be configured (in fact, "collaboration can be configured" itself is the flexibility we want ):

● For example, in the Observer mode, subject. registorobserver (observer) must be called to configure the collaboration between subject and observer.

● For example, to collaborate between filtergraph and videowindow in the multimedia framework DirectShow, you need to call filtergraph. setvideowindow (videowindow) to configure

Constructor is a typical configuration opportunity, for example:

Class cmywindow: Public cwnd
{
PRIVATE:
Cfiltergraph filtergraph;
Public
Cmywindow () {filtergraph. setvideowindow (this );};
};
-------------------------- Appendix ------------------------------------

Summary of basic knowledge

Table 1
Who Be called
Common functions Class: Fun ()
Constructor Superclass: superclass () ==> subclass: subclass ()
Destructor Subclass ::~ Subclass () => superclass ::~ Superclass ()

Table 2
Who Where Be called
Non-virtual functions Everywhere Class: Fun ()
Virtual Functions Common functions OBJ. vfun ()
Virtual Functions Constructor Class: vfun ()
Virtual Functions Destructor Class: vfun ()

It must be clear that [constructor/constructor] and [virtual/non-Virtual] are completely independent classification methods:
● As long as it is a "constructor/destructor", it will be "chaining )"
● As long as it is a "virtual function", it is possible to "obj. vfun ()"

They can take effect together but do not interfere with each other. For example, in the case of virtual destructor, see the following example:

class superclass{        virtual ~superclass() { println("superclass::~superclass()") };};class subclass : public superclass{        virtual ~subclass() { println("subclass::~subclass()") };};

Run

superclass  * super = new subclass();delete super;

The result is printed

subclass::~subclass()superclass::~superclass()

This means that when Delete super; is executed:
● Is it a chaining call? Yes. Because it is a destructor.
● Where does chaining call start? From subclass ::~ Subclass () => superclass ::~ Superclass (),
Because the actual object type of superclass * Super is subclass.

From: http://www.vckbase.com/document/viewdoc? Id = 933

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.