convert data type

what difference between 2 bellow code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include <iostream>
#include <string>
void test(double a)
{
    std::cout<<a+1<<std::endl;   
}
int main()
{
    
  unsigned int z;
  z=9;
  double x;
  x= static_cast<double>(z);
  test(x);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <iostream>
#include <string>
void test(double a)
{
    std::cout<<a+1<<std::endl;   
}
int main()
{
    
  unsigned int z;
  z=9;
  test((double)z);
}

when do you use each type?
> when do you use each type?

Why should one use either of the above? What is wrong with an implicit conversion?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

void test( double a ) { std::cout << a+1 << '\n' ; }

int main()
{
    const unsigned int z = 9 ;

    {
        const double temp = z ; // convert z to double (initialise an object)
        test(temp) ; // and call test with the double (lvalue)
    }

    test( double(z) ) ; // convert z to double via a cast and call test with the result

    test( static_cast<double>(z) ) ; // convert z to double via a cast and call test with the result

    // simplest; and usually preferred
    test(z) ; // implicitly convert z to double and call test with the result
}

http://coliru.stacked-crooked.com/a/866b9bdb1f5223b4
I don't disagree, but sometimes it may be worth exercising caution with implicit conversions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void test( int    z ) { cout << "This is " << z / 100 << " in test A\n"; }

void test( double z ) { cout << "This is " << z / 100 << " in test B\n"; }

void badTest( double &z ) { cout << "This is " << z / 100 << " in test C\n"; }

int main()
{
   int z = 10;

   test( z );
   test( (double)z );

// badTest( z );              // don't bother; it won't compile
// badTest( (double)z );      // nor this
}


This is 0 in test A
This is 0.1 in test B



Also, some other computing languages don't permit it.
But if you must use a cast in C++ use the C++ cast and stay away from the C style cast. The C++ style cast is easier to spot than the C style cast and is more explicit.
Hmmm. Okay so, srand() takes an unsigned int as argument. The argument could be implicitly determined by the compiler as well but yet most forumers in this forum (and let me mention: who are very particular with small details), as I have observed, explicitly cast it to unsigned int or size_t for the sake of being clear.

And let me mention that most of the times It's C-style castes (although it doesn't really matter for this case).

So if it were that using explicit castes reduced readability as compared to implicit type deduction, then why would so many forumers use it? I implied that castes were a good practice whenever and wherever appropriate. But it seems that it's not the case with all people as I learnt from another post where one person was willing to change all types for the sake of avoiding one cast. And let's say he's not a nub (unless 6k posts qualifies).

What are your preferences when it comes to casting?
What are your preferences when it comes to casting?

My preference is not to cast (either implicit, or explicit), unless there is no alternative.

Okay so, srand() takes an unsigned int as argument.

Then you should supply an unsigned int as the argument.

By the way if you're working in C++ you should consider working with std::random instead of srand()/rand().

And let me mention that most of the times It's C-style castes (although it doesn't really matter for this case).

Unless you're working in C you should prefer C++ style casts. They offer additional protection over C style casts.

I implied that castes were a good practice whenever and wherever appropriate.

I disagree, casts should be a method of last resort. When you cast the compiler assumes you know what you're doing and won't always inform you when you really don't.



Would you rather store integer values as doubles to avoid an implicit cast? But why?
Topic archived. No new replies allowed.