473,772 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor calling a constructor

How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}
Nov 16 '05 #1
4 1486
public class1(string Thing1) : this() {
// set a property to Thing1
}

"Roger Webb" <rw***@nos.twia .org> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}

Nov 16 '05 #2
Eco

Hi,

This is how you do it:

public class1
{
public class1()
{
// Initialization code
}

public class1(string thing1) : this()
{
this._thing1 = thing1;
}

//...

}

"Roger Webb" <rw***@nos.twia .org> escribió en el mensaje
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}

Nov 16 '05 #3
"Roger Webb" <rw***@nos.twia .org> wrote in message news:%2******** ********@tk2msf tngp13.phx.gbl. ..
How do you go about calling a constructor in the same class from another
constructor? (see below)

- Roger

ie.

public class1()
{
Does some general initialization
}

public class1(string Thing1)
{
/// Call class1() for general initialization
/// set a property to Thing1
}


I believe you could do something like so:

public class1(string Thing1) : this ()
{
/// set properties
}

This is very useful for designing overloaded types.

- carl
Nov 16 '05 #4
"Roger Webb" <rw***@nos.twia .org> wrote:
How do you go about calling a constructor in the
same class from another constructor?


Use the 'this' keyword (or 'base', to call the constructor of a base
class):

public Class1() { /* ... */ }
public Class1(string s) : this() { /* ... */ }

P.
Nov 16 '05 #5

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

Similar topics

3
4570
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
34
3115
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is this in any way used to create singletons. Can someone say how? Cheers, Andy
23
5184
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
9
2372
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
6
2465
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 (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying "invalid use of class". What is the correct syntax for calling a container...
5
2123
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If it finds a matching "Broom", i want it to return a "Broom" object to the calling program. I want the static method to call the constructor for the "Broom" class. I want this static method, (and one other called "CreateBroom") to be the only...
3
2338
by: Igor.Smirnov | last post by:
Hi, Why g++ in Linux does not call copy constructor at return statement? I have detected this at Sicentific Linux 4.1 (g++ v. 3.4.3) and Red Hat Linux 8.0 (g++ v. 3.2). Microsoft Visual Studio C++ v. 8 at Windows XP calls them as necessary (as should be according to Stroustrup and my previous experiense), that is it calls the copy constructor for the object, returned by
74
16036
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
1
8556
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
9
23769
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
0
9454
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
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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
9914
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
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5355
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
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2851
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.