1

I am learning static_cast<Type>(object) in C++. I found it calls copy constructor of Type with object. Is is true? If it is true, why it copies it? I thought it just change how to use the memory pointed by object. If it needs constructing copy, static_cast has more cost than I thought. Do I need to care of the cost?

The followings are the test code,

#include <iostream>

class Base {
public:
    Base() {};
    Base(const Base& org) {
        std::cout << "Base Copy Constructor" << std::endl;
    };
    virtual ~Base() {};
};

class Derived : public Base {
public:
    void static_casting(void) {
        static_cast<Base>(*this);
    }
};

void test_static_cast_copy_constructor(void) {
    Derived a;
    a.static_casting();
}

Thank you very much.

Anton Savin
  • 40,838
  • 8
  • 54
  • 90
mora
  • 2,217
  • 4
  • 22
  • 32
  • 3
    In case it wasn't obvious, [you're **slicing**](https://stackoverflow.com/questions/274626/what-is-object-slicing) the object with that cast. – WhozCraig Apr 02 '16 at 06:04
  • 2
    If you want to change how the memory is used, use `static_cast`. All remarks about slicing still apply. – Raymond Chen Apr 02 '16 at 06:05
  • 1
    "I thought it just change how to use the memory pointed by object." You thought wrong. In fact, in most cases `static_cast(object)` is *defined* as doing `Type value(object)` for some imaginary object `value`.That want it does by definition. That's what `static_cast` is for. – AnT stands with Russia Apr 02 '16 at 06:12
  • You are asking whether the copy constructor is used, why don't you put some output into it and try it yourself? Still, casting to an object type will create a new object. – Ulrich Eckhardt Apr 02 '16 at 06:12
  • Thank you WhozCraig. I did not know static_cast can slice the object. – mora Apr 02 '16 at 06:33
  • Thank you Raymond Chen for letting me know how to avoid constructing copy. – mora Apr 02 '16 at 06:42
  • Thank you AnT for telling what is static_cast deeply. Please let me ask a little bit more. Is static_cast is same as casting in C language? – mora Apr 02 '16 at 06:48
  • Thank you Ulrich Eckhardt for commenting. I supposed I ask Why ~ not Whether ~. My English might not be good. – mora Apr 02 '16 at 06:53

1 Answers1

1

It sounds like you're expecting static_cast to work like it does with pointers, but there is no pointer casting happening in your code. static_cast<Type *>(&object) would yield a pointer to Type, and dereferencing it would indeed let you treat object as though it were of type Type (I assume this is what you mean when you say "change how to use the memory pointed by object"). This is possible because object still exists in its original form somewhere in the memory, and the pointer in effect only provides access to a portion of object's data and behaviour.

However, as pointed out in the comments, static_cast<Type>(object) yields a new object of type Type - and since a new object is created, the appropriate constructor is called, with the portions of the copied object not contained in aType object chucked out (sliced).

  • Thank you for jaymmer. You are right. I expected all static_cast use object as a pointer. Thanks to you and guys above, I understand what is static_cast. And what I wanted is static_cast(object). Thank you again. – mora Apr 02 '16 at 07:01