c ++扩展同类的构造函数(没有继承)

时间:2015-06-22 22:53:58

标签: c++ constructor

我可能已经在这里找到了答案,但不过,我想确定一下。

我正在制作图形中表示的东西(因此是节点),我想知道构造函数的代码是否按我想象的方式工作。

G ++并没有抱怨。

我有以下课程:

#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()吗?

感谢您的帮助: - )

5 个答案:

答案 0 :(得分:4)

是的,你可以从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!

答案 1 :(得分:2)

这就是所谓的“委托构造函数”,它是在C ++ 11(2011年完成的语言修订版)中引入的。

答案 2 :(得分:2)

是的,您可以从C ++ 11开始委托其他构造函数。

使用C ++ 03,您必须采用其他方法,例如init函数和人工虚拟基类。

C ++ 11还引入了构造函数的继承,带有using声明,减少了常见简单情况下的样板量。

答案 3 :(得分:1)

This is an interesting read.

委托构造函数通常用于相反的方向可能是有意义的。

#include "node.hpp"

unsigned long Node::maxID = 0;

Node::Node():Node(nullptr)
{
}

Node::Node(Node*const& parent_p) : Node(parent_p, "")
{
}

Node::Node(Node*const& p, std::string const& Name)
{
    parent = p;
    name = Name;
    flags = 0;
    id = maxID++;
}

此特殊情况也可以使用默认参数轻松实现。

Node::Node(Node*p = 0, std::string const& Name = "")
{
    parent = p;
    name = Name;
    flags = 0;
    id = maxID++;
}

答案 4 :(得分:1)

他们是所谓的委托构造者。他们将类构造委托给其他构造函数。如果使用构造函数,则它应该是mem-initializer列表中唯一的初始化器。

考虑到将构造函数声明为此构造函数

的方法没有多大意义
Node(Node*const& parent_p);

更有意义地声明它就像

Node( const Node *parent_p );

否则,看起来parent_p指向的节点可以在构造函数中更改。

相关问题