I try to call another constructor in one constructor, but it doesn't work.
here is the exaple:
How can I do that? Thanks!Code:class A
{
A(int a);
A(int a, int b);
}
A::A(int a)
{
....
}
A::A(int a,int b)
{
A::A(a);
....
}
Printable View
I try to call another constructor in one constructor, but it doesn't work.
here is the exaple:
How can I do that? Thanks!Code:class A
{
A(int a);
A(int a, int b);
}
A::A(int a)
{
....
}
A::A(int a,int b)
{
A::A(a);
....
}
I don't think it's possible, but you can create a function which you can call from both constructors, and in this particular case you can use default parameter.
Calling another constructor from the same class is not possible, but like RoboTact said, use a init function and call that from all your constructors.
No I don't think either that it is possible. I think calling the constructor of a class from another constructor would just construct a new instance of the class.
You either write a private "init()" function or just rewrite the member initializer list everytime.
If that's appropriate for your situation, you can also just have one constructor that takes two int arguments, with a default value for the second one.
If you do this:
I reckon it compiles and constructs a temporary object of A constructed with a and then does nothing with it.Code:A::A( int a, int b )
{
A::A(a);
}
That's what it does in Visual C++ 6.0. In fact I changed it to this:Quote:
Originally Posted by NMTop40
and when I stepped through it in the debugger, the temporary object created by the line A::A(a) had its destructor called even before the call to f(). Comeau won't compile it at all.Code:A::A( int a, int b )
{
A::A(a);
f();
}
What's your point ? Why do you want to call two contructor ?
Let me try to answer for greghua. :)
I have been seeing quite a few posts asking if it is possible to call a constructor from another constructor within the same class. Most of time, the reason given is to eliminate the duplicate code for initialization in both constructors.
Although, calling another constructor may create another object, the constructor doesn't prevent us to call another function. Thus, you can have a common init() function to be called by both constructors.
Code:
A::A(int a, int b)
{
//...
init(a);
}
A::A(int a)
{
//...
init(a);
}
A::init(int a)
{
//...
}
Dear greghua,
I agree with Mr. Kheun's approach of calling an intermediate function from all constructors. Thats the approach I mostly use --- with the occational exception of using default values.
The trick is:
The <Default Value> would be the value you would be assigning to member b, if argument b were not supplied. This is equivalent to the two function version you have posted and is quite useful when you want to expand to muti-variable initialization. (But if the variable are too many, try considering the property fields instead).Code:class A
{
A(int a, int b = <Default Value> );
};
A::A(int a, int b)
{
....
}
When you cannot decide an appropriate default value, then you can fall back to the intermediate init() function mechanism.
Ofcourse, you can combine both approaches and that would give you the mechanism for "Reset" functionality for objects.
All the best.
Thanking you,
Yours,
P.GopalaKrishna.
A::A(a) is clearly not the normal syntax to create an object but is it right that the object deletes before the end of the function?Quote:
Originally Posted by Smasher/Devourer
Suppose that our class is an automatic mutex lock. I might want to create a temporary thus:
I would normally expect aMutex to be locked until the end of the function, even if I haven't assigned this temporary to any particular named object. ThusCode:void SomeFunction()
{
AutoMutexLock( aMutex );
// do code that needs the mutex locked
}
I would expect to do exactly the same thing, except that in the latter case I can access the lock through aLock.Code:void SomeFunction
{
AutoMutexLock aLock( aMutex );
// do code
}
Maybe someone who knows the standard well can answer this question.
Life gets much simpler if you stop thinking of constructors and destructors as callable entities. The standard (12.1/1) says "Constructors do not have names." You can't call something that doesn't have a name. (And use of the class name to identify the ctor or dtor is a "special declarator syntax", not a name.) I know that destructors are callable, but this should only be done under very specialised circumstances.
I just find it easier to mentally label ctors and dtors as "things that happen at construction and destruction". This way, you don't even think about doing things that you can't.
But when you construct a temporary should its scope last as long as it would if you had assigned it to a named variable (as with my mutex-locking example)?
Under Solaris I used to create temps this way and it worked but it doesn't work properly under VC++ (even 7.1) where you have to use a named variable to store the return type.
Dito. Did you even read my post? I said that it would only create a temporary instance of the class A. :rolleyes:Quote:
Originally Posted by Smasher/Devourer
:D Very true..very true...Nice quote...Cheers :thumb:Quote:
Originally Posted by Graham