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

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 1269
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*******@net4u.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.edu> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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.edu> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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
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...
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: 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...
6
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...
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...
9
by: Nicolas | last post by:
Hi, How can one call a constructor in the same class as the constructor itself ? Thanks.
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...
20
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...
1
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...
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:
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...
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
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
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...
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...
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.