473,662 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1444
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 = "DefaultNam e";
}

// 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(strin g 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*******@Yaho o.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
3654
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. And i have things to offer, and to request. And a lot of ideas, but who needs them.... here's an example (from type_struct.py):
3
2310
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 possible, it will take a long time, and I really don't care :) I need to go through all of the types, look for something within each, and do something with it if it contains what i'm looking for. So, is it possible to do this enumeration? Let me...
5
1264
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 destructor. In the managed world I find that I can not use this because destructors (and copy constructors) for value types are not allowed. Of all the restrictions when compiling managed C++ code (and there are many!) this one is one of my pet hates To...
4
5037
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 that they are not mutually exclusive of one another. The above classification assimilates my knowledge of having used constructors in both the above manners.
5
4569
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 constructors (because we do not allow developers to instantiate them directly). Because our business objects are instantiated very frequently, the idea of using reflection sounds like a performance killer (I haven't done any tests on this, but the...
6
3827
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 database for the given ID. I could just leave two separate constructors, or i could just have the default one call the second with a "-1" as the ID. This means that the initialization of certain fields can be based on whether my ID is -1 (for lazy...
12
2693
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
3962
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 built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.