class B
{
public:
B(){x=0;}
B(char* y){y=0;}
B(double z){z=0;}
B operator=(B h){};
~B(){};
private:
int x;
};
There is no any sense to assign values to parameters in bodies of constructors. Also it would be better if instead of char * the parameter would be declared as const char * because you use string literals as the argument.. Also data member x is not initialized in the last two constructors. So you could write simply
1 2
B( constchar* ) : x( 0 ){}
B( double ) : x( 0 ){}
Also it would be more correctly to defined the copy assignment operator the following way
There is no any sense to assign values to parameters in bodies of constructors.
Wrong. You should always perform unmanaged resource acquisition in the constructor body. If you do it in the initialization list and it throws, you're screwed big time.
If you overwrite an argument in the very beginning of the function then there is no any sense to pass it in the function because the argument simply is not used. It is obvious but not for you.:)
What is the sense in the following calls of constructor B(double z){z=0;}
B( 10.0 );
B( 0.5 );
and so on if neither 10.0 nor 0.5 is used in the constructor?
And moreover there is no any RAII. You even do not understand what is RAII. This parameter is a local variable that will be deleted after exiting from the constructor.
I seriously think you don't understand what JLBorges and I are discussing here. If you don't have anything relevant to add to the conversation, you should refrain from posting.
There is no any sense to assign values to parameters in bodies of constructors.
m4ster r0shi wrote:
Wrong. You should always perform unmanaged resource acquisition in the constructor body. If you do it in the initialization list and it throws, you're screwed big time.
What you've suggested is not at odds with what Vlad said. Take a look at the original code:
1 2 3 4 5 6 7
class B
{
public:
B(){x=0;}
B(char* y){y=0;}
B(double z){z=0;}
...
The initialization is being skipped entirely to assign to the parameters in the last two constructors.