You need to enable JavaScript to run this app. C++多重继承场景下派生类构造器如何复用基类构造器完成正确初始化
优惠活动
大模型
产品
解决方案
定价
更多

C++多重继承场景下派生类构造器如何复用基类构造器完成正确初始化

C++多继承场景下ProjectManager类构造实现问题

我们先查看如下类定义:

ProjectManager.hh

#ifndef INPUT_CPP_FILES_PROJECT_MANAGER_HH
#define INPUT_CPP_FILES_PROJECT_MANAGER_HH

#include <string>

#include "Employee.hh"
#include "Programmer.hh"
#include "Tester.hh"

namespace OrganizationNamespace {
    class ProjectManager : public Programmer, public Tester {
    public:
        ProjectManager():Employee(), Programmer(), Tester()
        {}

        explicit ProjectManager(const std::string& id):Employee(id), Programmer(), Tester()
        {}

        ProjectManager(std::string&id, std::string &name):Employee(id, name), Programmer(), Tester()
        {}

        ProjectManager(std::string &id, std::string &name, std::string &programming_language):Employee(id, name), Programmer(programming_language), Tester()
        {}

        ProjectManager(std::string &id, std::string &name, std::string &programming_language, std::string &tool):Programmer(programming_language), Tester(tool),Employee(id, name)
        {}

        ProjectManager(ProjectManager const & pm)
        {
            this->id_ = pm.id_;
            this->name_ = pm.name_;
            this->programming_language_ = pm.programming_language_;
            this->tool_ = pm.tool_;
        }

        void print() const {
            std::cout << "(" << Employee::id_ << ", " << Employee::name_ << ", " << Programmer::programming_language_ << "," << Tester::tool_ << ")"
                      << std::endl;
        }
    };
}
#endif //INPUT_CPP_FILES_PROJECT_MANAGER_HH

main.cpp

#include "Employee.hh"
#include "Programmer.hh"
#include "ProjectManager.hh"

int main()
{
    std::string id  = "id";
    std::string name  = "name";
    std::string programming_language  = "programming-language";
    std::string testing_tool  = "testing-tool";

    OrganizationNamespace::ProjectManager pm(id, name, programming_language, testing_tool);
    pm.print();
}

实际运行输出

C:\Users\pc\source\repos\input_cpp_files\cmake-build-debug\input_cpp_files.exe
(id, name, ,)

Process finished with exit code 0

可观察到程序未输出正确结果。

预期输出

C:\Users\pc\source\repos\input_cpp_files\cmake-build-debug\input_cpp_files.exe
(id, name, programming-language, testing-tool)

Process finished with exit code 0

核心问题:应当如何实现ProjectManager类的构造器,才能得到正确的输出结果?

附加源码

Employee.hh

#ifndef INPUT_CPP_FILES_EMPLOYEE_HH
#define INPUT_CPP_FILES_EMPLOYEE_HH

#include <iostream>
#include <string>

namespace OrganizationNamespace {
    class Employee {
    protected:
        std::string id_;
        std::string name_;

    public:
        Employee() : id_ (""), name_("") {}

        Employee(const std::string &id) : id_(id), name_("") {}

        Employee(const std::string &id, const std::string &name) : id_(id), name_(name) {}

        Employee(Employee const & emp)
        {
            this->id_ = emp.id_;
            this->name_ = emp.name_;
        }

        void print() const
        {
            std::cout << "(" << id_ << ", " << name_ << ")" << std::endl;
        }
    };
}
#endif //INPUT_CPP_FILES_EMPLOYEE_HH

Programmer.hh

#ifndef INPUT_CPP_FILES_PROGRAMMER_HH
#define INPUT_CPP_FILES_PROGRAMMER_HH

#include "Employee.hh"

namespace OrganizationNamespace {
    class Programmer : public virtual Employee {
    protected:
        std::string programming_language_;

    public:
        Programmer() : Employee(), programming_language_("") {}

        Programmer(std::string id) : Employee(id) {}

        Programmer(std::string id, std::string name) : Employee(id, name) {}

        Programmer(std::string id, std::string name, std::string programming_language) : Employee(id, name),
                                                                                         programming_language_(
                                                                                                 programming_language) {}

        void print() const {
            std::cout << "(" << id_ << ", " << name_ << ", " << programming_language_ << ")" << std::endl;
        }
    };
}

#endif //INPUT_CPP_FILES_PROGRAMMER_HH

Tester.hh

#ifndef INPUT_CPP_FILES_TESTER_HH
#define INPUT_CPP_FILES_TESTER_HH

#include <string>

#include "Employee.hh"

namespace OrganizationNamespace {

    class Tester : public virtual Employee {
    protected:
        std::string tool_;

    public:
        Tester() : Employee(), tool_("") {}

        Tester(std::string id) : Employee(id) {}

        Tester(std::string id, std::string name) : Employee(id, name) {}

        Tester(std::string id, std::string name, std::string tool) : Employee(id, name), tool_(tool) {}

        void print() const {
            std::cout << "(" << id_ << ", " << name_ << ", " << tool_ << ")" << std::endl;
        }
    };
}
#endif //INPUT_CPP_FILES_TESTER_HH

内容的提问来源于stack exchange,提问作者user366312

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.03 05:54:03