How to call one ctor in another overloaded ctor?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
class Test{}
{
Test(int tmp) { lots of code here }

Test() { Test(0); }
}

Laser Lu said:
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!
 
Eddie Pazz said:
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)
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top