Constructor (object-oriented programming): Difference between revisions

Content deleted Content added
Formatting
m clean up; http->https (see this RfC) using AWB
Line 200:
==== C# static constructor ====
 
In [[C Sharp (programming language)|C#]], a ''static constructor'' is a static data initializer. Static constructors are also called ''class constructors''. Since the actual method generated has the name ''.cctor'' they are often also called "cctors".<ref>{{cite web|url=http://ericlippert.com/2013/02/06/static-constructors-part-one/ |title=Fabulous Adventures in Coding |publisher=Eric Lippert |date=2013-02-06|accessdate=2014-04-05}}</ref><ref>{{cite book|url=httphttps://books.google.com/books?id=oAcCRKd6EZgC&pg=PA222&lpg=PA222&dq=ctor+cctor&source=bl&ots=KAYfbDu_ay&sig=gLj6X9SpWXz86GHMvljkfb-D8Oc&hl=en&sa=X&ei=fUFAU--WJOewsASQ54GQAw&ved=0CGoQ6AEwCQ#v=onepage&q=ctor%20cctor&f=false |title=Expert .NET 2.0 IL Assembler |publisher=APress |date=2006-01-01|accessdate=2014-04-05}}</ref>
 
Static constructors allow complex static variable initialization.<ref>[http://msdn.microsoft.com/en-us/library/k9x6w0hc%28VS.80%29.aspx Static Constructor in C# on MSDN]</ref>
Line 237:
In [[C++]], the name of the constructor is the name of the class. It returns nothing. It can have parameters like any [[Method (computer programming)|member function]]. Constructor functions are usually declared in the public section, but can also be declared in the protected and private sections, if the user wants to restrict access to them.
 
The constructor has two parts. First is the [[initializer list]] which follows the [[parameter (computer science)|parameter list]] and before the method body. It starts with a colon and entries are comma-separated. The initializer list is not required, but offers the opportunity to provide values for data members and avoid separate assignment statements. The initializer list is required if you have ''const'' or reference type data members, or members that do not have parameterless constructor logic. Assignments occur according to the order in which data members are declared (even if the order in the initializer list is different).<ref>httphttps://stackoverflow.com/questions/1242830/constructor-initialization-list-evaluation-order Constructor</ref> The second part is the body, which is a normal method body enclosed in curly brackets.
 
C++ allows more than one constructor. The other constructors must have different parameters. Additionally constructors which contain parameters which are given default values, must adhere to the restriction that not all parameters are given a default value. This is a situation which only matters if there is a default constructor. The constructor of a [[base class]] (or base classes) can also be called by a derived class. Constructor functions are not inherited and their addresses cannot be referenced. When memory allocation is required, the ''new'' and ''delete'' operators are called implicitly.