473,396 Members | 1,996 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.

How to call one ctor in another overloaded ctor?

Suppose that we have a class with two overloaded ctors defined. And in one ctor, I have written a greate deal of code. So I'd like to reuse those code in another ctor avoiding rewrite them
My question is that, whether it is possible to invoke one ctor in another ctor? How to do it? Any example would be appreciated

Nov 16 '05 #1
3 3799
class Test{}
{
Test(int tmp) { lots of code here }

Test() { Test(0); }
}

"Laser Lu" <an*******@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
Suppose that we have a class with two overloaded ctors defined. And in one ctor, I have written a greate deal of code. So I'd like to reuse those code
in another ctor avoiding rewrite them. My question is that, whether it is possible to invoke one ctor in another ctor? How to do it? Any example would be appreciated!

Nov 16 '05 #2
Laser Lu <an*******@discussions.microsoft.com> wrote:
Suppose that we have a class with two overloaded ctors defined. And
in one ctor, I have written a greate deal of code. So I'd like to
reuse those code in another ctor avoiding rewrite them.
My question is that, whether it is possible to invoke one ctor in
another ctor? How to do it? Any example would be appreciated!


See http://www.pobox.com/~skeet/csharp/constructors.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Eddie Pazz <dr****@hotmail.com> wrote:
class Test{}
{
Test(int tmp) { lots of code here }

Test() { Test(0); }
}


That's Java syntax. In C#, the latter constructor would be:

Test() : this(0)
{
}

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

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

Similar topics

14
by: marbac | last post by:
Hello, i defined an overloaded operator = for my class which works perfect outside of my class. In this case, is following ctor valid? test::test (const test& src){ *this=src; }
10
by: sjbrown8 | last post by:
I have the piece of code below, and when i try compiling with the line g++ 753075304.cpp I get the following error message: 753075304.cpp: In function 'int main()': 753075304.cpp:29: error:...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
6
by: hazz | last post by:
I have a class/constructor hiearchy that functions correctly as illustrated below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client instantiates object D, eg. D m_D = new...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
3
by: paolo.dx | last post by:
Hello, I have to call a member funtion "myclass.myfunc()" from another function "void method();" but I don't know the name of "myclass" and "myfunc()" because I'm writing a library.h that has to be...
9
by: toton | last post by:
Hi, Which one is the correct syntax for constructor for static memory allocation Object obj("param"); or Object obj = Object("param"); 1) For the first one, how compiler checks it as not a...
18
by: cmk128 | last post by:
hi 1 #include <stdio.h> 2 3 class A{ 4 private: 5 int x; 6 public: 7 A(int x){ 8 this->x;
2
by: Marcin Gil | last post by:
First of all thanks for your patience and good pointers for Visitor and Curiously Recurring Template patterns last time :) Now another problem of mine. THE CODE #include <iostream>...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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,...
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...

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.