473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling other constructor of same class

Lets say I have a situation like this:

public class a {
* * * * public a () {
* * * * * * * * //...
* * * * }
** * * * public a (int i) {
* * * * * * * * //...
* * * * }
}

Is there a way to call a() from within a(int i)? Basicly what I'm trying
to achieve is to put all the common code in a (), and in a(int i) just
initialize what I can with the i parameter and than call the a()
construcotor. Would that be possible?

--
"Stara boljka se leci starim lekom...
Dabome vinom, ta nebi valjda mlekom?"
Nov 16 '05 #1
5 1284
Nikola Skoric wrote:
Lets say I have a situation like this:

public class a {
public a () {
//...
}
public a (int i) {
//...
}
}

Is there a way to call a() from within a(int i)? Basicly what I'm trying
to achieve is to put all the common code in a (), and in a(int i) just
initialize what I can with the i parameter and than call the a()
construcotor. Would that be possible?


no, you want do do something like this:

public class a
{
public a()
{
function_call() ;
}

public a(int i)
{
// stuff with i;
fuction_call();
}

private void function_call()
{
// the common functionality of a() and a(int)
}
Nov 16 '05 #2
"Nikola Skoric" <ni*******@net4 u.hr> wrote:
Is there a way to call a() from within a(int i)?


public a(int i) : this()
{
// Code dealing with i, to follow the () constructor
}

P.
Nov 16 '05 #3
Pat,

Wondering why can not we call default ctor from a(int i) ?

Thanks.

"Pat Richey" <ri****@uiuc.ed u> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
Nikola Skoric wrote:
Lets say I have a situation like this:

public class a {
public a () {
//...
}
public a (int i) {
//...
}
}

Is there a way to call a() from within a(int i)? Basicly what I'm trying
to achieve is to put all the common code in a (), and in a(int i) just
initialize what I can with the i parameter and than call the a()
construcotor. Would that be possible?


no, you want do do something like this:

public class a
{
public a()
{
function_call() ;
}

public a(int i)
{
// stuff with i;
fuction_call();
}

private void function_call()
{
// the common functionality of a() and a(int)
}

Nov 16 '05 #4
Nikola Skoric wrote:
Lets say I have a situation like this:

public class a {
public a () {
//...
}
public a (int i) {
//...
}
}

Is there a way to call a() from within a(int i)? Basicly what I'm trying
to achieve is to put all the common code in a (), and in a(int i) just
initialize what I can with the i parameter and than call the a()
construcotor. Would that be possible?


Only if you want to call it at the very beginning. Attaching " :
this()" to the end of the constructor method instructs the compiler to
call the overloaded constructor with the proper signature. The second
constructor is called *before* the current constructor. It works
similar to how "public a() : base() " works.

public class a {
public a()
{
}

public a ( int i ) : this()
{
this.i = i;
}
}

--Mike
Nov 16 '05 #5
Try this...

public a() {
}

public a(int I) : this.a() {
}

Chang wrote:
Pat,

Wondering why can not we call default ctor from a(int i) ?

Thanks.

"Pat Richey" <ri****@uiuc.ed u> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
Nikola Skoric wrote:
Lets say I have a situation like this:

public class a {
public a () {
//...
}
public a (int i) {
//...
}
}

Is there a way to call a() from within a(int i)? Basicly what I'm trying
to achieve is to put all the common code in a (), and in a(int i) just
initialize what I can with the i parameter and than call the a()
construcotor . Would that be possible?


no, you want do do something like this:

public class a
{
public a()
{
function_call ();
}

public a(int i)
{
// stuff with i;
fuction_call( );
}

private void function_call()
{
// the common functionality of a() and a(int)
}


Nov 16 '05 #6

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

Similar topics

4
9634
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to populate with a reference to the object that constructed this current object. i would attempt to accomplish this by setting the appropriate field from within the constructor... i figured it might be obtainable from the stack trace, but that...
4
7293
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 constructor again. In my larger program, I find that the member variables don't get re-initialized when "reconstructed". I don't have that problem in this demo, but the second time the constructor is called, its "this" points to a different location. ...
4
1549
by: Girish Shetty | last post by:
> Hi All, > > Some Strange thing with C++ Constructer !! > I never knew that we can initialize an object / a variable more than once > using C++ Constructer till I started doing some RnD coding!!!!
6
2109
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
4
2239
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 the first constructor can set the Provider property and invoke the resetConnection() method first, before the second constructor assigns the connection string to the Connection property, but I can't seem to work out the syntax.
9
2672
by: Nicolas | last post by:
Hi, How can one call a constructor in the same class as the constructor itself ? Thanks.
2
3221
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 (b) While using the Factory pattern
20
3235
by: lars.uffmann | last post by:
I have to work on a rather big project that a bunch of people wrote and that has no useful documentation at all, my C++ has rusted in a bit, now I stumbled over a constructor that looks something like the below and have no idea what to do with it :) inheritedClass::inheritedClass () : originalConstructor () , m_cells(cells), m_srfcs(srfcs) {
1
8558
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?
7
2691
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 name based on the name of the VB6 application. A second choice would be a file name based on the # COM interface assembly. I have tried calling Assembly.GetCallingAssembly() but this fails when I use the VB6 client. Is there a way to get this...
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9591
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
10594
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...
0
10087
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
9166
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
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
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...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
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.