C++ 通过指针更新派生类对象中的变量
C++ Updating a variable in object of derived class via pointer
我正在构建一个链表,其中节点都链接到 Head。 Head 派生自节点,但 Head 需要指向最后一个节点的指针。 请参阅代码顶部的注释。
/* Base <= node <= node <= node
* | ^
* | ptr to last node |
* -------------------------
*/
class Node {
private:
Node* prev;
public:
explicit Node(Node* parent) : prev(parent) {
Node* foo_ptr = this;
while (foo_ptr->prev != 0) {
foo_ptr = foo_ptr->prev;
}
// foo_ptr points to Base, how can I now change Base::last?
}
};
class Base : public Node {
private:
Node* last;
public:
Base() : Node(0), last(this) {}
};
添加新节点时如何更改更改变量Base::last
,例如:
Node* n = new Base;
new Node(n); // can Node constructor update n->last?
我正在考虑使用虚函数来更新变量,但根据这篇文章:在构造函数中调用虚拟函数,这是一个不行的,所以我不想这样做。 那么有没有实现这种类型的链表的好方法呢?
谢谢。。。
http://coliru.stacked-crooked.com/a/213596aa1ffe7602
我添加了一个标志值,以便我们可以判断我们实际上访问了Base
类:
#include <iostream>
class Node {
private:
Node* prev;
public:
inline void changeBaseLast(Node* base);
explicit Node(Node* parent) : prev(parent) {
Node* foo_ptr = this;
while (foo_ptr->prev != 0) {
foo_ptr = foo_ptr->prev;
}
// foo_ptr points to Base
// now change Base::last
changeBaseLast(foo_ptr);
}
int data;
};
class Base : public Node {
private:
Node* last;
public:
int flag;
Base() : Node(0), last(this), flag(0) {}
};
//Here, we can see that we change the base_ptr to 1.
void Node::changeBaseLast(Node* base) {
Base* base_ptr = static_cast<Base*>(base);
base_ptr->flag=1;
}
int main() {
Node* n = new Base;
new Node(n);
std::cout << static_cast<Base*>(n)->flag << std::endl;
}
如果拉出引用派生类的部分,然后将其内联,则应该没有问题。但请注意,在定义派生类之后,我需要定义引用派生类的函数。
如果您确定最后一个节点将始终是Base
对象,那么使用 static_cast<Base*>
可能还不错。
class Base : public Node {
...
// Factory method to create child nodes
Node* getNode(Node* parent) {
Node* newNode = new Node(parent);
last = newNode;
return newNode;
}
}
这个应该更容易理解并且仍然使用static_cast,因为您想通过基类进行追加。
class Node {
private:
Node* prev;
public:
explicit Node() : prev{nullptr} { }
void setParent(Node *parent) {
prev = parent;
}
};
class Base : public Node {
private:
Node* last;
public:
Base() : Node{}, last{this} { }
void append(Node *node) {
node->setParent(last);
last = node;
}
};
int main() {
Node* n = new Base;
static_cast<Base*>(n)->append(new Node{});
}
无论如何,我不明白基类的必要性。您不能简单地在某处(例如结构)存储两个指针,一个用于列表的头部,一个用于最后一个节点?
相关文章:
- 你能重载对象变量名本身返回的内容吗
- 为什么我可以使用 memcpy 将一个对象变量复制到另一个对象变量
- 如何使用std::lower_bound比较对象变量,而不使用第二个对象进行比较
- 将私有对象变量与用户输入的变量进行比较
- 具有已定义操作重载的 C++ 非类型化值对象/变量库
- 如何对对象数组进行排序,而不考虑对象变量类型
- 在功能中设置对象变量
- 如何创建对象变量,以便每次分配新的指针
- C++ FileIO 以读取和写入对象变量
- 私下声明的对象变量的突变器/访问器方法
- C++ 列表中的对象变量在迭代过程中不会更新
- 稍后使用私有对象变量时如何初始化数组(初始化对象后)
- 通过向量更改对象变量的值
- C 全局对象变量内存释放
- 基本数据类型变量 VS 对象变量
- C 复制分配运算符,用于参考对象变量
- c++从列表中移除元素,并将其分配给对象变量
- 我们是否需要在C++中同步局部对象变量
- 点运算符和箭头运算符之间的区别 结构对象变量 在 C 或 C++ 中创建树
- 无法访问在 std 回调中传递的对象变量