Constructors

Hello, today i made this little program. I'd like to know if you think it's well done.

I've to define a class B with constructors so that I can compile the following main:

1
2
3
4
5
6
7
8
9
10
int main()
{
    B d,e,f;
    
    d=3;
    e="tre";
    f=3.0;
    
    return 0;
}


Here behind it's my code, it seems to works but I'd like to know if it's all correct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdlib>
#include <iostream>

using namespace std;

class B
{
      public:
             B(){x=0;}
             B(char* y){y=0;}
             B(double z){z=0;}
      
             B operator=(B h){};  
             
             ~B(){}; 

      private:
              int x;
             
};
      

int main(int argc, char *argv[])
{
    B d,e,f;
    
    d=3;
    e="tre";
    f=3.0;
    
    return 0;
}


Thanks for your attentions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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( const char* ) : x( 0 ){}
             B( double ) : x( 0 ){}


Also it would be more correctly to defined the copy assignment operator the following way

B & operator =( const B &){}
Last edited on
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.
Last edited on
Your post doesn't make sense at all.

Here's my source -> http://www.gotw.ca/gotw/066.htm
Clear, I've now understood what I wrong, thanks everybody !
@m4ster r0shi


It looks like that your brain is totally absent. You even are unable to understand what you have been said.

No, you are the one who doesn't understand. Read my post again.
@m4ster r0shi +1

Yes.

Ideally always use RAII, and there is no issue.

If for some reason that can't be done, perform unmanaged resource acquisition in the constructor body, not in initializer lists.
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.
Last edited on
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.
You both are saying a stupidy. You simply understand nothing.
Or maybe you are not smart enough to understand what we're saying (or rather, doing).
Last edited on
vlad from moscow wrote:
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.
What you've suggested is not at odds with what Vlad said.

Obviously. I was doing it on purpose.
Topic archived. No new replies allowed.