Share c++ source type is not polymorphic

thuydiem917

New member
#C ++ #polymorphism #Source #type #compiler

** Loại nguồn C ++ không đa hình **

Trong C ++, loại nguồn là loại được khai báo trong tệp nguồn.Các loại nguồn không phải là đa hình, điều đó có nghĩa là chúng không thể được sử dụng theo cách đa hình.Điều này có nghĩa là bạn không thể tạo một con trỏ sang loại nguồn và sau đó đúc nó sang một loại khác.

Ví dụ: mã sau sẽ không biên dịch:

`` `C ++
int* i = new int;
gấp đôi* d = (gấp đôi*) i;// Lỗi: Không thể chuyển đổi từ 'int*' sang 'gấp đôi*'
`` `

Lý do cho điều này là trình biên dịch không thể biết tại thời điểm biên dịch loại con trỏ `Tôi 'đang chỉ ra.Nó có thể chỉ ra một `int`,` float` hoặc bất kỳ loại nào khác.Do đó, trình biên dịch không thể đảm bảo rằng các diễn viên từ `int*` đến `Double*` là an toàn.

Nếu bạn cần tạo một con trỏ sang loại đa hình, bạn có thể sử dụng `std :: shared_ptr` hoặc` std :: độc đáo_ptr`.Những con trỏ thông minh này sẽ tự động quản lý tuổi thọ của đối tượng mà chúng đang chỉ ra, và chúng cũng sẽ ngăn bạn đúc con trỏ sang một loại không tương thích.

Dưới đây là một ví dụ về cách sử dụng `std :: shared_ptr` để tạo một con trỏ đa hình:

`` `C ++
std :: Shared_ptr <base> b = mới có nguồn gốc;
std :: shared_ptr <dẫn xuất> d = std :: Dynamic_pulum_cast <Derive> (b);// an toàn
`` `

Trong ví dụ này, chúng tôi tạo một `std :: shared_ptr` thành một đối tượng` base`.Sau đó, chúng tôi sử dụng chức năng `std :: Dynamic_pulum_cast` để chuyển con trỏ lên đối tượng` dẫn xuất '.Điều này là an toàn vì chức năng `std :: Dynamic_pulum_cast` sẽ chỉ thành công nếu con trỏ thực sự chỉ vào một đối tượng` deriving`.

## 5 hashtags

* #C ++
* #polymorphism
* #Nguồn
* #Kiểu
* #compiler
=======================================
#C++ #polymorphism #Source #type #compiler

**C++ Source Type is not polymorphic**

In C++, a source type is a type that is declared in a source file. Source types are not polymorphic, which means that they cannot be used in a polymorphic way. This means that you cannot create a pointer to a source type and then cast it to a different type.

For example, the following code will not compile:

```c++
int* i = new int;
double* d = (double*)i; // error: cannot convert from 'int*' to 'double*'
```

The reason for this is that the compiler cannot know at compile time what type the pointer `i` is pointing to. It could be pointing to an `int`, a `float`, or any other type. Therefore, the compiler cannot guarantee that the cast from `int*` to `double*` is safe.

If you need to create a pointer to a polymorphic type, you can use a `std::shared_ptr` or a `std::unique_ptr`. These smart pointers will automatically manage the lifetime of the object that they are pointing to, and they will also prevent you from casting the pointer to an incompatible type.

Here is an example of how to use a `std::shared_ptr` to create a polymorphic pointer:

```c++
std::shared_ptr<Base> b = new Derived;
std::shared_ptr<Derived> d = std::dynamic_pointer_cast<Derived>(b); // safe
```

In this example, we create a `std::shared_ptr` to a `Base` object. We then use the `std::dynamic_pointer_cast` function to cast the pointer to a `Derived` object. This is safe because the `std::dynamic_pointer_cast` function will only succeed if the pointer is actually pointing to a `Derived` object.

## 5 Hashtags

* #C++
* #polymorphism
* #Source
* #type
* #compiler
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top