gpt4 book ai didi

c++扩展同一类的构造函数(无继承)

转载 作者:太空狗 更新时间:2023-10-29 23:39:43 25 4
gpt4 key购买 nike

我可能已经在这里的某个地方找到了我的答案,但尽管如此,我还是想确定一下。

我正在做一些用图表表示的东西(因此是节点),我想知道构造函数的这段代码是否按照我想的方式工作。

G++ 不会提示。

我有以下类(class):

#ifndef viper_node
#define viper_node

#include "../globals.hpp"

#include <vector>
/**
* @brief The base class for the nodes
*/
class Node {
public:
/**
* @brief base constructor for the node
*/
Node();

/**
* @brief exteded constructor for the node
* @param [in] parent_p the pointer to the parent of the new node
*/
Node(Node*const& parent_p);
/**
* @brief extended^2 constructor for the node
* @param [in] parent_p the pointer to the parent of the new node
* @param [in] name the name of the node
*/
Node(Node*const& p, std::string const& name);
/**
* @brief base destructor
*/
~Node();

protected:
/// pointer to the parent node of this one (nullptr if rootnode)
Node* parent;

///pointers to the children
std::vector<Node*> children;

///the name of the class/func/var (ex: children)
std::string name;

///description of the name/func/var (ex: pointers to the children)
std::string description;

///the properties of the node (static, private,...)
uint flags;

/// the type of the node (function, variable, binary, etc.)
nodeType node_type;

///the scope of the node (global, class member, function local)
nodeScope scope;

unsigned long get_id() {return id;};

private:
///the id of the node (unique)
unsigned long id;

///to keep track of the next unused id
static unsigned long maxID;

};

#endif

和以下定义:

#include "node.hpp"

unsigned long Node::maxID = 0;

Node::Node()
{
parent = nullptr;
flags = 0;
id = maxID++;
}

Node::Node(Node*const& parent_p) : Node::Node()
{
parent = parent_p;
}

Node::Node(Node*const& p, std::string const& Name) : Node::Node(p)
{
name = Name;
}

Node::~Node()
{
parent = nullptr;
for (auto it : children)
{
delete it;
}
}

我的问题是:
如果我调用 Node(parent_p,"name"),函数前面是 Node(parent_p),它本身前面是 Node()

感谢您的帮助:-)

最佳答案

是的,您可以从 C++11 标准开始。 Wiki article .

也是一个快速的经验验证:

using namespace std;

class A
{
public:
A()
{
cout << "Hello ";
}

A(int x) : A()
{
cout << "World!" << endl;
}
};

int main()
{
A a(1);


return 0;
}

打印:

Hello World!

关于c++扩展同一类的构造函数(无继承),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com