Calling a constructor in its own class

  • Thread starter Thread starter Nicolas
  • Start date Start date
N

Nicolas

Hi,

How can one call a constructor in the same class as the constructor itself ?

Thanks.
 
How can one call a constructor in the same class as the constructor itself ?

class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}



Mattias
 
Mattias said:
class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}

I'm sorry that won't solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from
another constructor of the same class)
 
if you mean like

C c1 = new C();
C c2 = c1.Clone();

then Clone will look like

public C Clone() {
C result = new C();
return result;
}
ottomh
--
Grace + Peace,
Peter N Roth
Engineering Objects International
http://engineeringobjects.com
Home of Matrix.NET
 
Just call it like you would from anywhere else....

class Foo
{
public void SomeFunc()
{
Foo foo = new Foo();
// etc....
}
}


Unless you are retrying to restructure the object itself, inwhich case, you
don't want to call a constructor at all (the object already exists), but a
initializer:

class Foo
{
public Foo(int n, string s)
{
Set(n,s);
}

public SomeFunc()
{
Set(5, "Hello World");
}

private void Set(int n, string s)
{
// So what ever you want here.
}
}

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Are you trying to "re-initialize" your instance using a specific constructor?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
class Foo
{
public Foo(string s) { ... }
public Foo(int i) : this(i.ToString()) {}
// like this -> ^^^^^^^^^^^^^^^^^^^^
}

I'm sorry that won't solve my problem, because what I need to do is to
call a constructor from another method of the same class (not from
another constructor of the same class)
 
Richard said:
Are you trying to "re-initialize" your instance using a specific constructor?

That's it exactly. I know that simply calling an init() method from both
the constructor and the place where I want to re-init is an option, but
I am just curious to see whether it's possible to do it with a ctor.
 
That's it exactly. I know that simply calling an init() method from both
the constructor and the place where I want to re-init is an option, but
I am just curious to see whether it's possible to do it with a ctor.

No. Ctors create something out of nothing. To allow ctors to do this
would be an abasement of the concept.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Peter N Roth said:
=======================================
[erstwhile VC++ MVP] <<<<<<<<<<<<<<<< ??? =======================================

Hi James - why'd you "switch"?


In my new day job, I do C# instead of C++. And the questions one finds
in the C++ newsgroup were getting less interesting -- mostly dreary DLL or
memory issue.

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Back
Top