本文介绍了“衍生的数组”不是一种“基数”阵列。题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 黑客攻击者, 我有一个继承自Node类的BuildNode类。 同样,我有一个继承的类BuildTree来自树类。 树包含一个成员变量: vector< Node>节点; //为清楚起见,让它为orig_nodes BuildTree包含一个成员变量: vector< BuildNode>节点; //为了清楚起见,让它成为build_nodes 据我所知,这会重载节点,定义两个不同的 "节"变量而不是build_nodes clobber orig_nodes(因为 是我的意图)。 现在,让我们说我定义了一个Tree方法: bool Tree :: is_degenerate const {return nodes.empty(); } 如果我有一个BuildTree对象build_tree,我调用 build_tree.is_degenerate(),它返回orig_nodes.empty(),不是" build_nodes.empty()"按需要。 所以问题是: 有没有办法在Tree中定义引用节点的方法,并且 让那些方法对build_nodes进行操作如果调用对象是一个 BuildTree和orig_nodes如果调用对象是树,没有 复制BuildTree中的代码? [我想也许可以制作一个虚拟的 方法get_nodes(),它在Tree对象中返回orig_nodes,并在 a中,BuildTree对象返回build_nodes,然后每当我通常使用节点时调用get_nodes() 在树方法中。这个 有用吗?] 我不介意重构我的代码来做到这一点,如果有人可以 建议一个重新设计这些对象的好方法。 子问题:有两个节点在 BuildNodes对象中具有相同名称的对象是一种非常好的方式来拍摄自己的脚。 (我很惊讶代码工作了很久以前我今天早些时候发现了这个问题 。)有没有一个解决方案我可以完全取消这个 名称冲突? Best, JOSEPH 解决方案 不确定你的意思。没有名字冲突。 BuildTree'''节点'' _hides_ Tree'''节点''。在BuildTree内部,基类''''节点'确实不存在,基本上(除了占用一些空间)。 Victor 如果不了解你想要解决的问题的更多信息,那么很难给出具体的建议。上面两个一般的想法之一 可能有用,但我可以很容易地想到这两种情况都不会有用。 - Greg Schmidt gr***@trawna.com Trawna出版物 http://www.trawna.com/ 我不认为我听到过clobber这个词。与C ++构造有关。Overload,override和hide等。成员们彼此做了什么。 我已经看过(并被逗乐了)它与C一起使用:数据库手册 库,在几个函数的描述中警告:确保你 分配足够的缓冲区空间,否则内存将被破坏。 我可能会有它说''覆盖'',但是,嘿,我得到了消息的b / b 。这可能是唯一存在的软件相关文件 几十次使用clobber一词。 :-) -Mike Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable:vector<Node> nodes; // For clarity, let this be "orig_nodes" BuildTree includes a member variable:vector<BuildNode> nodes; // For clarity, let this be "build_nodes" So as far as I can tell, this overloads "nodes", defining two different"nodes" variables instead of having build_nodes clobber orig_nodes (aswas my intention). Now, let''s say I have a Tree method defined:bool Tree::is_degenerate const { return nodes.empty(); } If I have a BuildTree object build_tree, and I callbuild_tree.is_degenerate(), it returns "orig_nodes.empty()", not"build_nodes.empty()" as is desired. So the question is:Is there any way to define methods in Tree that reference "nodes", andget those methods to act upon "build_nodes" if the calling object is aBuildTree and "orig_nodes" if the calling object is a Tree, withoutduplicating the code in BuildTree? [I was thinking maybe make a virtualmethod "get_nodes()", which in a Tree object returns orig_nodes and ina BuildTree object returns "build_nodes", and then call get_nodes()whenever I would normally use "nodes" in Tree methods. Would thiswork?] I don''t mind refactoring my code to get this right, if anyone cansuggest a good way to redesign these objects. Sub-question: Having two "nodes" objects with the same name in aBuildNodes object is a really good way to shoot oneself in the foot.(I''m surprised the code worked for so long before I found this issueearlier today.) Is there a solution in which I can do away with thisname clash entirely? Best,JOSEPH 解决方案 Not sure what you mean. There is no name clash. BuildTree''s ''nodes''_hides_ Tree''s ''nodes''. Inside BuildTree the base class'' ''nodes'' doesnot exist, essentially (except that it takes up some space). Victor Without knowing more about the problem you''re trying to solve, it''s veryhard to give concrete suggestions. One of the two general ideas abovemight work, but I can easily think of cases where neither would beuseful. --Greg Schmidt gr***@trawna.comTrawna Publications http://www.trawna.com/ I don''t think I heard the term "clobber" in relation to C++ constructs. "Overload", "override", and "hide" is what members do to each other. I''ve seen (and been amused by) it used with C: The manual for a databaselibrary, in descriptions of several functions warned: "Be sure youallocate sufficient buffer space, or memory will be clobbered".I would have probably had it say ''overwritten'', but hey, I gotthe message. That was probably the only software-related documentin existence that used the term ''clobber'' dozens of times. :-) -Mike 这篇关于“衍生的数组”不是一种“基数”阵列。题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-19 17:16