I have done this code, basically what I wanted to do is set a static data member and use it as a default constructor..thing that I achieved...the constructor that I'm using I have got it on a book which explain everything clearly...in the contructor declaration the arguments are assigned to 0
this in concret..
complex(int a = 0,int b = 0);
Apparently what the ceros make the compiler picks the default......and I dont understand that, why???...I used always that as a default constructor like something like this
1 2
int a {};
int b {};
but apparently not...it's taking the constructor to get the default value if not argument is provided...
here is my whole code, could anyone tell me what's going on??
If a has a non-zero value it will pick a, otherwise it will pick default_complex.r, and assign it to r.
This is not a very good solution because it gives trouble if someone want r and i to be zero. If zeroes are passed to the constructor it will not assign the zeroes but use the values from default_complex.
1 2 3
// Will set n.i = default_complex.i and n.r = default_complex.r
// but what if I want n.i = 0 and n.r = 0 ?
complex n(0,0);
I think a better solution would be to use the default_complex values as default arguments, and then in the constructor you simply initialize the member variables r and i with the values of the parameters the normal way.
complex(int a = default_complex.r, int b = default_complex.i);
That's what I didn't get perfectly I know how a ternary works "in general" but i didn't know that Iit meant if a has non-zero valueit picks a ,otherwise it will pick default_complex.r.
My idea was to be able to declare complex object with non-argument, one argument ad two argument...then if i don't provide any argument at any complex variable, it will pick the default...i dont know if the constructor that you have shown will be able to admit complex(4) for instance...but I'll know soon, I'll check it out know...
thanks, but about the ternary.....I always though that It was if this(condition) is true get first if it's not true(condition) get second...there must be a relation with the declaration
The ternary works independetly from what int a and int b has a default...if complex( int a = 0,int b = 0) is default....and yes, it's obvious, the default_number can be (0,0) and it will get from the default what is not send though the arguments...
but then a ternary like this
1 2 3 4 5 6 7
void test(int a){
int b = 7;
b = a ? a:b;
This doesn't behave like if a is cero get b .....
The other ternary was behaving like that because it was in a cosntructor then??
about the ternary.....I always though that It was if this(condition) is true get first if it's not true(condition) get second...there must be a relation with the declaration
That's how it works, but here you don't have a bool (true or false). Instead you have an integer. If you use an integer as a condition it will treat 0 as false and all other values as true. This is not just the ternary operator. If conditions and loop conditions work the same way.
So another way of writing it is like this:
r = a != 0 ? a : default_complex.r;
Winsu wrote:
a ternary like this
1 2 3 4
void test(int a){
int b = 7;
b = a ? a:b;
This doesn't behave like if a is cero get b .....
It does.
Winsu wrote:
The other ternary was behaving like that because it was in a cosntructor then??
No, the ternary operator works the same way wherever you use it.
ok, that's true It's doing a boolean equation but transforming integers in booleans, so any int less cero is true and a cero is false, so true not equal than false.....thanks!!,
I knew that, I knew that you can pass an integer to a boolean it was what happened, but I was seeing it as a comparation with integers not with booleans.... thanks!!