mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Fix T.84 example code
This commit is contained in:
parent
c7c5614a42
commit
4b78a00ff4
|
@ -15774,18 +15774,19 @@ Avoid code bloat.
|
|||
It could be a base class:
|
||||
|
||||
struct Link_base { // stable
|
||||
Link* suc;
|
||||
Link* pre;
|
||||
Link* suc = nullptr;
|
||||
Link* pre = nullptr;
|
||||
};
|
||||
|
||||
template<typename T> // templated wrapper to add type safety
|
||||
struct Link : Link_base {
|
||||
explicit Link(const T& v) : val(v) { }
|
||||
T val;
|
||||
};
|
||||
|
||||
struct List_base {
|
||||
Link_base* first; // first element (if any)
|
||||
int sz; // number of elements
|
||||
Link_base* first = nullptr; // first element (if any)
|
||||
int sz = 0; // number of elements
|
||||
void add_front(Link_base* p);
|
||||
// ...
|
||||
};
|
||||
|
@ -15794,7 +15795,7 @@ It could be a base class:
|
|||
class List : List_base {
|
||||
public:
|
||||
void put_front(const T& e) { add_front(new Link<T>{e}); } // implicit cast to Link_base
|
||||
T& front() { static_cast<Link<T>*>(first).val; } // explicit cast back to Link<T>
|
||||
T& front() { static_cast<Link<T>*>(first)->val; } // explicit cast back to Link<T>
|
||||
// ...
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user