In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "why C++ cannot assign pointers to derived class arrays to base class pointers". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Never assign a pointer to a derived class array to a base class pointer
Reason
Subscripting the resulting base pointer will lead to invalid object access and probably to memory corruption.
Subscript operations on base class pointers as a result of assignment cause invalid object accesses and possible memory corruption.
Example
struct B { int x; };
struct D : B { int y; };
void use(B*);
D a[] = {{1, 2}, {3, 4}, {5, 6}};
B* p = a; // bad: a decays to &a[0] which is converted to a B*
p[1].x = 7; // overwrite D[0].y
use(a); // bad: a decades to &a[0] which is converted to a B*Enforcement
Flag all combinations of array decay and base to derived conversions.
Prompt for all array degeneration and base class type to derived class type conversions.
Pass an array as a span rather than as a pointer, and don't let the array name suffer a derived-to-base conversion before getting into the span
Use span to pass arrays instead of pointers, and don't let array names undergo a derived class conversion to base class types before placing them in span.
"C++ Why can't the derived class array pointer assigned to the base class pointer" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.