473,395 Members | 1,574 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,395 software developers and data experts.

Calling a constructor from another

How does a constructor of one class call another of the same class?
When would this mechanism make sense or be required?
PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern

Nov 17 '05 #1
2 3820

"Sathyaish" <Sa*******@Yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
How does a constructor of one class call another of the same class?
public MyClass() : this(<arguments>)
{
....
}

basically. You can also use base() to call a constructor on a base class When would this mechanism make sense or be required?


Generally, this allows you to specialize your constructors without redoing
code. Imagine you have a parameterless constructor that sets a few
properties to default values and one with a parameter that only sets one
property. By calling the parameterless constructor from your single
parameter one you bypass needing to re-write the code to set those
properties.
Nov 17 '05 #2
Sathyaish <Sa*******@Yahoo.com> wrote:
How does a constructor of one class call another of the same class?
See http://www.pobox.com/~skeet/csharp/constructors.html
When would this mechanism make sense or be required?

PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern


I wouldn't expect either of these to use this pattern. I'd expect both
of them to have static methods/properties/initialisers which called the
constructor, but there's no need to have two constructors there.

It makes sense to have multiple constructors when there are several
parameters, some of which can take defaults. For instance:

public Foo() : this (0, 0)
{
}

public Foo (int x, int y)
{
this.x = x;
this.y = y;
}

allows defaulting of x and y to 0 by overloading. Usually in this kind
of situation all constructors end up calling the most specific one,
passing in default values.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

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

Similar topics

6
by: Asfand Yar Qazi | last post by:
Hi, Consider this: class C { int j; public: C(int i) : j(i)
2
by: William Payne | last post by:
Hello, consider these following two classes. A base class, class MDIChildWindow, and a class inherting from that base class, class Document. In the static base member function callback() I obtain a...
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
4
by: Mike Labosh | last post by:
Greetings, all: I want to invoke one constructor from another to gain code reuse. In the snip below, I want my second constructor to pass its first argument to the first constructor so that...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
2
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
6
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.