Talk:Constructor (object-oriented programming)

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

This statement is inaccurate for C++[edit]

"constructor (sometimes shortened to ctor)" In C++, a ctor is NOT a constructor. It is one of a series of initializing expressions that are written in the header-portion of a constructor method. They are performed, in order, before the body of the constructor is executed. — Preceding unsigned comment added by 108.197.177.10 (talk) 00:42, 28 October 2012 (UTC)[reply]

Is a constructor really a method?[edit]

I'd rather propose to say: a constructor is an operation AND a method is an operation, too. I.E. a method may have different return types whereas a constructor's return type should be clear. You might define a constructor as a special method with a constraint about its return type but defining them as different metaclasses seems to be a better (and proper) solution.

Constructors should not be considered as a method as the declaration syntax is very different from normal methods, and it cannot be called by other methods directly (it can be called indirectly by creating a new instance object). T4bits 15:24, 8 November 2005 (UTC)[reply]

Constructors in other languages[edit]

Does anyone know about constructors in other languages? It would be a great help, especially since the current article is mostly based on C++

Hello. I added a php one.. hope it helps. :-)

Totally inaccurate and doesn't cite sources[edit]

I hope the authors realize there are more than 4 languages out there and many constructors return the object itself as a return value. Also there are no sources or citations. --Quirex 03:02, 10 December 2006 (UTC)[reply]

FWIW, I agree. I updated one sentence, and left what I believe was an erroneous statement in place (stating that constructors return no value, not even void) because I don't consider myself an OO expert, just a programmer and grammarian! But, constructors often return pointers to a new object, right? Chris van Hasselt (talk) 19:18, 15 April 2009 (UTC)[reply]

Functional programming[edit]

This article proceeds as-if constructors are a feature peculiar to OOLs, but constructors are also a feature of functional languages, such as Haskell. — SlamDiego 05:52, 13 December 2006 (UTC)[reply]

Constructors in Haskell have a different origin, being related to function symbols in a term algebra, and IMO are not related to object-oriented constructors. They are not user-defined functions for initializing objects. Note that type constructors are constructors. 130.126.141.87 (talk) 02:09, 7 November 2009 (UTC)[reply]

Constructors that use inheritence[edit]

I have moved this section from the article as it has a number of problems that should be sorted first. The areas that could be improved are:

  • use of parent/child uncommon terminology for class inheritance
  • dubious programming practice in the example
  • example code formatting
  • code in the penultimate paragraph won't compile

Rich257 13:19, 4 January 2007 (UTC)[reply]

In fact there is the mistaken notion that a constructor which accepts values for variables in the super-class can only set them by way of a constructor in the super-class. But otherwise hidden variables could be set by accessors in the super-class (which accessors could be hidden from all but the super-class and its sub-classes), and less hidden variables could beset more directly by the sub-class. I suggest that this whole section be discarded.SlamDiego 02:56, 5 January 2007 (UTC)[reply]

Constructors that use inheritance[edit]

Constructors can use a parent/child system, in a similar way to that of classes. if a class "student" is a child of class "Person" then any variables that student uses (which are contained within person, ie, name, age, sex) will require a constructor in person in order to use them. This can be done with a default constructor (one that requires no parameters) or it can be done by calling the person constructor, on the same line as the definition for the student constructor.

C++[edit]

Example[edit]
//person class
public class person
{
public:
int mAge;
char* mSex;
String* mName;
person(int age, char* sex, String* name)
{
mAge=age;
mSex=sex;
mName=name;
}
};
//student class
public class student : public person
{
public:
String* mSchool;
//student constructor containing a call to person constructor.
student(int age, char* sex, String* name, String* school) : person(age,sex,name)
{
mSchool=school;
}
};

using this code, a new student can be made in this way.

student fred= new student(18,"M","fred flintstone","Exeter College");

when this is done the constructor for student is called, then before "school" is set, person is called and age,sex and name are set.

Virtual Constructors[edit]

Hi... Can any one tell me why virtual constructors do not exist whereas virtual destructors exist??? will be of great help.. —Preceding unsigned comment added by 210.211.241.24 (talkcontribs) 19:47, 7 May 2007

When you are constructing an object, you know exactly what type it is (it is the type of the constructor). When you are destructing a dynamically-allocated object using the delete operator on a pointer to it, you are not necessarily sure what type the object is; because you might be using a pointer to a base class. So to call the correct destructor, the destructor needs to be virtual. --Spoon! 23:07, 17 October 2007 (UTC)[reply]

Public constructors vs. private constructors[edit]

Suppose that a class is public. Does it matter whether the constructor is public or private? Sarsaparilla (talk) 00:28, 11 February 2008 (UTC)[reply]

Of course. It depends on the design of the class whether it has public, protected or private constructors or, possibly, a combination of these. Rich257 (talk) 08:51, 11 February 2008 (UTC)[reply]

Move[edit]

Could some registered user please move this page to Constructor_(object-oriented_programming)? Thanks. --203.206.56.51 (talk) 12:13, 14 March 2009 (UTC)[reply]

I agree this move should be made! I was just going to suggest it because this article is nothing more than an survey of how algebraic data type constructors are implemented in a few OO programming languages. Pcap ping 15:56, 17 August 2009 (UTC)[reply]

Objective-C [class new] vs. [[class aloc] init]

I'd to have a section on the two step constructors used in some languages like objective-c. A quick reference: http://stackoverflow.com/questions/719877/obj-c-why-is-alloc-init-used-instead-of-new. —Preceding unsigned comment added by 68.190.118.182 (talk) 01:26, 6 January 2010 (UTC)[reply]

Incorrect statement for C++[edit]

inside the C++ chapter, the following statement is incorrect:

Assignments occur according to the order of the initializer list.

The correct statement is: "Assignments occur according to the order in which data members are declared (even if the order in the initializer list is different)." [1]

Remark: It is good practice to list data members in the initializer list in the same order they are declared into the class (and we can get a warning from the compiler if it is not the case), so the 2 orders are usually the same.

Etienne Boireau (talk) 11:42, 12 November 2014 (UTC)[reply]

If I have no remark within 1 week, I will correct the article accordingly...

Etienne Boireau (talk) 14:02, 21 November 2014 (UTC)[reply]

References

Memory organization[edit]

I'm not convinced that a section on memory allocation is appropriate for this article, since constructors don't generally deal with memory allocation. Constructors are really about initialization, and allocation is a separate issue. Even if there are some languages that require a constructor to do both jobs (though I am not aware of any), this is really a quirk of the particular language and is not the norm for constructors.

Additionally, the current section on memory organization is blatantly wrong when it says that reference types are allocated on the heap while value types are allocated on the stack. Value types are allocated wherever they need to be (which may be on the stack, or may be inside another object on the heap). Additionally, even reference types can be allocated on the stack if the condition are right for certain optimizations. The real difference between reference types and value types is their semantics, not where they exist (which is why they're not called heap types and stack types).

Kwvanderlinde (talk) 04:23, 23 July 2016 (UTC)[reply]

India Education Program course assignment[edit]

This article was the subject of an educational assignment supported by Wikipedia Ambassadors through the India Education Program.

The above message was substituted from {{IEP assignment}} by PrimeBOT (talk) on 19:59, 1 February 2023 (UTC)[reply]