Calling a constructor is possible or not?
I have two classes with same namespace. I want to call the constructor of one class inside of a function which is in 开发者_如何学Canother class. Is this possible?
Simply using the new
keyword calls the constructor for the class.
if you want to construct object of another class then simply do this (anywhere.. even in your function):
Class2 obj = new Class2();
that's all.
Just remember the constructor should have a public access.
like:
class Class2
{
public Class2()
{
}
}
If they are in the same namespace, then new OneClass()
is all you need.
Yes. Why wouldn't it be possible?
Although I'd go with the terminology "construct" over "call constructor".
Yes of course! When you create a new instance of the class, its constructor is invoked (assuming it is a public non-static constructor). You can read more about constructors here - http://msdn.microsoft.com/en-us/library/k9x6w0hc(VS.80).aspx
精彩评论