Question

int main()
{
    class_name object;
    object.method();
    fstream file("writeobject.dat" , ios::out|ios::app);
    file.write(reinterpret_cast<char*>(&object), sizeof(object));
    return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////

int main()
{
    class_name object;
    object.method();
    fstream file("writeobject.dat" , ios::out|ios::app);
    file.write((char*)&bk,sizeof(book));
    return 0;
}

What is the difference between above both functions. What is reinterpret_cast is doing here? I don't see any of the difference between output of both main() functions.

Was it helpful?

Solution

A C style cast is nothing but the C++ cast that succeeds from the predefined order of:

  • const_cast
  • static_cast
  • static_cast, then const_cast
  • reinterpret_cast
  • reinterpret_cast, then const_cast

In this case they are doing the same thing. However, if you are using C++ it is better to use C++ style of explicit casting because they are more indicative of the intent and also it's always better to be explicit about what casting you need than be at the mercy of the compiler to chose one for you.

OTHER TIPS

There is no functional difference between the C-style cast and the reinterpret_cast in the above case.

However the reinterpret_cast might be considered to be preferred by many because it is explicit in what it is doing, both to the compiler and the other humans reading the code.

Being explicit to the compiler is valuable in cases where automatic conversions might take place at times when it is not desired. Consider:

class Foo 
{
public:
    operator double() const
    {   
        return mD; 
    }   
    Foo () : mD (4.12) {}; 
private:
    double mD; 
};

int main()
{
    Foo foo;
    double d = (double) foo;
    double d2 = reinterpret_cast <double> (foo);

}

The code:

double d = (double) foo;

compiles, and when it is run the conversion operator operator double() is called. However the reinterpret_cast will not compile because Foo cannot be converted to a double.

To carry the "be explicit" philosophy forward, in cases where you do want the automatic conversion to be available, you can use a static_cast:

double d3 = static_cast <double> (foo);

This will again call the conversion operator.

Aside from the good answers already given, reinterpret_cast is MUCH easier to search for in the code than (char *), which may occur in other places, never mind that a regex type search will need some escaping to not interpret the * as a wildcard.

More importantly, if you want to find EVERY place that you cast something from one pointer type to another, finding all reinterpret_cast is quite easy, where finding all variants of (int *), (char *), (uint8_t *) and (foo **) would be quite a bit of effort to come up with the right regex to match all those without missing something out and not adding some extra finds that you didn't want.

One uses a C++-style reinterpret_cast, the other a C-style cast. The C++ style is better because it's more explicit about what it's doing, and highlights a potentially dangerous operation with verbose syntax.

In this case they do the same as for this combination of target and source type reinterpret_cast is used for the c-style cast. But just a slight change could change that.

Say you had

const class_name object;

and the first form would not compile while the second switched to "reinterpret_cast, then const_cast". And if the function you pass it to actually modified the data, you'd discover that only at runtime.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top