Talk:Constructor (computer science)
From Wikipedia, the free encyclopedia
Is a constructor really a method? 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)
Contents |
[edit] Constructors in other languages
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. :-)
[edit] Totally inaccurate and doesn't cite sources
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)
[edit] Functional programming
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)
[edit] Constructors that use inheritence
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)
- 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)
[edit] Constructors that use inheritance
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.
[edit] C++
[edit] Example
//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.
[edit] Virtual Constructors
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 (talk • contribs) 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)
[edit] Public constructors vs. private constructors
Suppose that a class is public. Does it matter whether the constructor is public or private? Sarsaparilla (talk) 00:28, 11 February 2008 (UTC)