Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value cast vs Reference cast

Tags:

c++

c++11

What is the difference between value cast and reference cast? Why one of them invokes conversion (aka creating new object) and other doesn't? What are caveats of using casting on rhs?

Assume this is Derived . Why those will not actually cast to Base?

*this = (Base&) rhs
(Base)* this = rhs

Could you please show on simple examples?

like image 825
rakamakafo Avatar asked May 06 '18 16:05

rakamakafo


People also ask

What is the difference between dynamic and static cast?

static_cast − This is used for the normal/ordinary type conversion. This is also the cast responsible for implicit type coersion and can also be called explicitly. You should use it in cases like converting float to int, char to int, etc. dynamic_cast −This cast is used for handling polymorphism.

Can you reference a static cast?

You can use static_cast to downcast lvalues to references of derived classes, because there's no implicit cast for that way (as opposed to converting from a derived to a base class).

What is a function style cast?

Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); }

What are the new style cast operators in C++?

This chapter discusses the new cast operators in the C++ standard: const_cast , reinterpret_cast , static_cast and dynamic_cast . A cast converts an object or value from one type to another.


1 Answers

Value cast creates a new value from an existing one; reference cast creates a new reference to the same existing value.

Reference cast neither changes the content of an existing object nor creates a new one; it is restricted to changing the interpretation of the value that is already there. Value casting, on the other hand, can make a new object from an existing one, so it has fewer restrictions.

For example, if you have an unsigned char and you want a value or a reference of type int, value cast is going to work, while reference casting is going to fail:

unsigned char orig = 'x';
int  v(orig); // Works
int &r(orig); // Does not work

rhs is Derived, I want to assign all inherited and non-inherited stuff from rhs into Base

Then you need to cast both sides to Base, or add an assignment operator to Derived that takes a const Base& as an argument. Here is an example of casting on both sides (may be hard to understand by other programmers reading your code)

struct Base {
    int x;
    Base(int x) : x(x) {}
};
struct Derived1 : public Base {
    Derived1(int x) : Base(x) {}
};
struct Derived2 : public Base {
    Derived2(int x) : Base(x) {}
};

Running the code below

Derived1 d1(5);
Derived2 d2(10);
cout << d1.x << " " << d2.x << endl;
((Base&)d1) = (Base&)d2;
cout << d1.x << " " << d2.x << endl;

produces the following printout:

5 10
10 10

As you can see, the assignment ((Base&)d1) = (Base&)d2 copied the content of d2's Base portion into d1's Base portion (demo).

like image 121
Sergey Kalinichenko Avatar answered Oct 19 '22 22:10

Sergey Kalinichenko