Home > Backend Development > C++ > Why Does `static_cast` to a Derived Pointer Seem to Access the Entire Derived Object in C When the Original Pointer Points to a Base Class Object?

Why Does `static_cast` to a Derived Pointer Seem to Access the Entire Derived Object in C When the Original Pointer Points to a Base Class Object?

DDD
Release: 2024-11-26 15:20:10
Original
354 people have browsed it

Why Does `static_cast` to a Derived Pointer Seem to Access the Entire Derived Object in C   When the Original Pointer Points to a Base Class Object?

Downcasting with Static Cast in C

In C , downcasting refers to the conversion of a base class pointer or reference to a derived class pointer or reference. One way to perform downcasting is through the use of the static_cast<> operator.

Consider the following code:

class base {
    base();
    virtual void func();
};

class derived : public base {
    derived();
    void func();
    void func_d();
    int a;
};

int main() {
    base *b = new base();
    sizeof(*b); // Gives 4.
    derived *d = static_cast<derived*>(b);
    sizeof(*d); // Gives 8- means whole derived obj size..why?
    d->func_d();
}
Copy after login

In this example, we have a base class base and a derived class derived. We create a base class pointer b pointing to an object of type base. Using static_cast<>, we attempt to cast b to a derived class pointer d.

The confusing question here is why the derived class pointer d has access to the entire derived class object, despite b pointing to a base class object.

However, it's crucial to understand that using static_cast<> to cast a pointer to a type that it doesn't belong to is considered undefined behavior in C .

According to the C standard ([expr.static.cast]), a static cast to a derived class pointer from a base class pointer is only allowed if the following conditions are met:

  1. There exists a valid standard conversion from "pointer to D" to "pointer to B" (where D is the derived class and B is the base class).
  2. B is not a virtual base class of D or a base class of a virtual base class of D.
  3. If the base class object pointed to is actually a subobject of a derived class object, the resulting pointer should point to the enclosing derived class object.

If any of these conditions are not met, the result of the cast is undefined.

In the given code, since b points to a pure base class object, there is no enclosing derived class object to point to. Therefore, the behavior of the program becomes unpredictable and should not be relied upon.

The above is the detailed content of Why Does `static_cast` to a Derived Pointer Seem to Access the Entire Derived Object in C When the Original Pointer Points to a Base Class Object?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template