473,399 Members | 3,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Types of constructors

What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.

Jul 21 '05 #1
3 1428
I private constructor is a constructor that is only available to the
same class. They can be useful in factory scenarios, where a class
creates other instances of its own class and you don't want other
classes to be able to instantiate the class in the same way.

For example:

public class Foo {
string name;

// this is the only constructor available to other classes
// other classes cannot set the name field directly
public Foo(){
this.name = "DefaultName";
}

// this constructor is not available to other classes
// it allows the name field to be set
private Foo(string name){
this.name = name;
}

// other classes can call this factory method
public Foo CreateFoo(string fooName){
// calls the private constructor
// and returns a new Foo with the name "fooName"
return new Foo(fooName);
}

public string Name { get { return this.name; } }
}
You can also have static constructors, which can be used to initialize
static fields of the class.
static Foo() { // initialize static fields }
Joshua Flanagan
http://flimflan.com/blog

Sathyaish wrote:
What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.

Jul 21 '05 #2
Thank you, Joshua. I understood it. That was a nice explanation.

Jul 21 '05 #3
"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
What is a private constructor, and why would a class have one? What are
the other kinds of constructors besides:

(1) public constructors; and
(2) parameterized constructors

And I understand that they are not mutually exclusive of one another.
The above classification assimilates my knowledge of having used
constructors in both the above manners.


In addition to Joshua's explanation, they are also useful to create a static
class. This is generally a sealed class with only a private constructor.
Unlike a factory class, it does not provide methods to create the class, and
only provides static methods.
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
3
by: Cliff Harris | last post by:
I have an odd question that I'm hoping someone can help with. I simply (or not simply?) need to enumerate through all of the data types in the .Net framework. I do understand that if this is...
5
by: PaulW | last post by:
Personally, I like to use simple abstractions to wrap resources For example consider a class that wraps a resource - say an IntPtr which is allocated in the constructor and deallocated in the...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.