Should you employ this pointer in the constructor?, C/C++ Programming

Should you employ this pointer in the constructor?

Posted Date: 3/15/2013 6:01:46 AM | Location : United States

A: Some of people feel you must not use the pointer in a constructor since the object is not fully formed yet. Though, you can use this in the constructor (in the {body} and even in the initialization list) if you are careful.

Here is something which always works: the {body} of a constructor (or a function called from the constructor) can access reliably the data members declared in a base class and/or the data members declared in the constructor''s own class. It is because all of those data members are guaranteed to have been completely constructed by the time the constructor''s {body} starts executing.

Here is something which never works: the {body} of a constructor (or a function called from the constructor) can''t get down to a derived class via calling a virtual member function i.e. overridden in the derived class. If your aim was to obtain to the overridden function in the derived class, you won''t obtain what you wish. Note down that you won''t get to the override in the derived class independent of how you call virtual member function: explicitly via the this pointer (for example this->method()), implicitly using this pointer (for example method()), or even calling some other function which calls the virtual member function onto your this object. The bottom line is this: even if the caller is building an object of a derived class, throughout the constructor of the base class, your object is not still of that derived class.

Here is something which sometimes works: if you pass any data members in this object to another data member''s initialize, you have to ensure that the other data member has already been initialized. You can determine whether the other data member has (or has not) been initialized by using some straightforward language rules which are independent of the particular compiler you''re utilizing. The bad news is that you ought to know those language rules (for example: base class sub-objects are initialized first (look up the order if you have multiple and/or virtual inheritance!), then data members described in the class are initialized in the order wherein they appear in the class declaration). If you don''t know about these rules, then don''t pass any data member from the object (regardless of whether or not you use explicitly this keyword) to any other data member''s initializer! And if you do know about the rules, be careful please.


Posted by | Posted Date: 3/15/2013 6:02:15 AM

Related Discussions:- Should you employ this pointer in the constructor?, Assignment Help, Ask Question on Should you employ this pointer in the constructor?, Get Answer, Expert's Help, Should you employ this pointer in the constructor? Discussions

Write discussion on Should you employ this pointer in the constructor?
Your posts are moderated
Related Questions
good night i have an assignment that i really need help with. i have to create a struct file with tag name book to have members int book_id char isbn[255]

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

The rPeANUt simulator provides buffering of the characters that are typed into the terminal. However, this would generally not be the case so if the program did not consume a chara

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

The procedure +, * and list take arbitrary numbers of arguments. One way to define such a procedure is to use define with dotted-tail notation. In a procedure definition, a paramet


What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

a padovan string p(n) for a natural number n is defined as p(0)=''x'' p(1)=''y'' p(2)=''z'' p(n)=p(n-2)+p(n-3),n>2

Objective:  Construct a C program that controls the LCD display, and is capable of displaying strings.  Add functions to the C program that allows more control over the display.