Content deleted Content added
Adding link to orphaned article, Wikiproject Orphanage: You can help! |
m cleaning up, typo(s) fixed: newly- → newly using AWB |
||
Line 2:
{{ProgLangCompare}}
In [[class-based programming|class-based]] [[object-oriented programming]], a '''constructor''' in a [[Class (computer programming)|class]] is a special type of [[subroutine]] called to [[object creation|create an object]]. It prepares the new object for use, often accepting [[argument]]s that the constructor uses to set required [[member variable]]s.
'''Instance constructors''', sometimes referred to as '''.ctor''', are used to create and initialize any instance member variables when the new expression is used to create an object of a class. To initialize a static class, or static variables in a non-static class, a '''static constructor''' must be defined. Static constructors are sometimes referred to as '''.cctor'''.<ref>{{cite web|url=http://msdn.microsoft.com/en-us/library/k6sa6h87.aspx|title=Instance Constructors (C# Programming Guide) |publisher=Microsoft Developers Networks |accessdate=2014-04-05}}</ref><ref>{{cite
A constructor resembles an [[method (computer science)|instance method]], but it differs from a method in that it has no explicit [[return type]], it is not implicitly [[inheritance (object-oriented programming)|inherited]] and it usually has different rules for scope modifiers. Constructors often have the same name as the declaring [[class (computer science)|class]]. They have the task of [[initialization (computing)|initializing]] the object's [[data member]]s and of establishing the [[invariant (computer science)|invariant]] of the class, failing if the invariant is invalid. A properly written constructor leaves the resulting [[object (computer science)|object]] in a ''valid'' state. [[Immutable object]]s must be initialized in a constructor.
Line 61:
=== Move constructors ===
In C++, [[C++11#
== Syntax ==
Line 70:
* In [[Moose perl|Moose object system]] for Perl, constructors (named ''new'') are automatically created and are extended by specifying a ''BUILD'' method.
* In [[Visual Basic .NET]], the constructor is called "<code>New</code>".
* In [[Python (programming language)|Python]], the constructor is split over two methods, "<code>__new__</code>" and "<code>__init__</code>". The <code>__new__</code> method is responsible for allocating memory for the instance, and receives the class as an argument (conventionally called "<code>cls</code>"). The <code>__init__</code> method (often called "the initialiser") is passed the newly
* [[Object Pascal]] constructors are signified by the keyword "<code>constructor</code>" and can have user-defined names (but are mostly called "<code>Create</code>").
* In [[Objective-C]], the constructor method is split across two methods, "<code>alloc</code>" and "<code>init</code>" with the <code>alloc</code> method setting aside (allocating) memory for an instance of the class, and the <code>init</code> method handling the bulk of initializing the instance. A call to the method "<code>new</code>" invokes both the <code>alloc</code> and the <code>init</code> methods, for the class instance.
|