【问题标题】:C++ Unable to assign base class "this" pointer the derived class object inside base class constructorC ++无法将基类“this”指针分配给基类构造函数内的派生类对象
【发布时间】:2021-06-25 16:16:56
【问题描述】:

您好,我正在学习 C++,并尝试使用以下构造将基类 *this 指针分配给派生类对象。我的问题是,这种方式在 C++ 中是否可行?因为,当我尝试编译它时,我得到了错误


错误:“Derived_1”之前的预期类型说明符


如果这样的事情是可能的,那么正确的方法是什么。我这样做的目的是通过基类自动获取相对派生类实现,基于 DISPLAY 类型,以避免 main 中的 if-else 类子句。任何帮助将不胜感激。

#ifndef _MAIN_HPP_
#define _MAIN_HPP_

#include <iostream>

enum class DISPLAY {
    DERIVED_1 = 1,
    DERIVED_2 = 2
};

class Base {
    private:
        int variable = 0;
    public:
        Base(){std::cout<<"Base Class Empty Constructor"<<std::endl;}
        Base(DISPLAY Type) {
            std::cout<<"Base Class Constructor Derived_"<<static_cast<int>(Type)<<std::endl;
            switch(Type) 
            {
                case(DISPLAY::DERIVED_1): {
                    *this = new Derived_1();
                }break;
                case(DISPLAY::DERIVED_2): {
                    *this = new Derived_2();
                }break;
            }
        }
        virtual ~Base() {std::cout<<"This is Base Class Destructor"<<std::endl;}
        virtual int Sum(int x, int y) {return x+y;};
};

class Derived_1 : public Base {
    public:
        Derived_1(){std::cout<<"This is Derived_1 Class Constructor"<<std::endl;}
        virtual ~Derived_1() {std::cout<<"This is Derived_1 Class Destructor"<<std::endl;}
        int Sum(int x, int y) {return (x*2)+(y*3);}
};

class Derived_2 : public Base {
    public:
        Derived_2(){std::cout<<"This is Derived_2 Class Constructor"<<std::endl;}
        virtual ~Derived_2() {std::cout<<"This is Derived_2 Class Destructor"<<std::endl;}
        int Sum(int x, int y) {return (x*1)+(y*2);}
};
#endif

#include "main.hpp"

using namespace std;

int main(int argc, char **argv)
{
    Base *base = new Base(DISPLAY::DERIVED_1);
    cout<<"SUM:"<<base->Sum(2, 10)<<endl;
    return 0;
}

错误摘要:

/home/junaid/workspace/Learn_Cpp/main.hpp: In constructor ‘Base::Base(DISPLAY)’:
/home/junaid/workspace/Learn_Cpp/main.hpp:21:33: error: expected type-specifier before ‘Derived_1’
   21 |                     *this = new Derived_1();
      |                                 ^~~~~~~~~
/home/junaid/workspace/Learn_Cpp/main.hpp:24:33: error: expected type-specifier before ‘Derived_2’
   24 |                     *this = new Derived_2();
      |                                 ^~~~~~~~~

Build finished with error(s).
The terminal process terminated with exit code: -1.

【问题讨论】:

  • 将构造函数实现移出线,您的类在您目前拥有代码时不存在。然后你会遇到对象切片问题。
  • 这不是 C++ 中类派生的工作方式。 Base 构造函数构造 Base 对象,而不是 Derived_1Derived_2 对象。您可以使用工厂模式解决此问题。
  • @AlanBirtles 你能告诉我将构造函数移出线是什么意思吗?
  • @RaymondChen 我现在明白为什么它不能将派生类对象分配给这个指针。但是,我会看看工厂模式是否可以做类似的事情。
  • 这并没有解决问题,但是以下划线开头的名称后跟大写字母 (_MAIN_HPP_) 和包含两个连续下划线的名称保留供实现使用。不要在你的代码中使用它们。

标签: c++ pointers inheritance this


【解决方案1】:

你绝对不能做你正在做的事情。你不能这样做:

                *this = new Derived_1();

我什至不想考虑这有什么问题。你想要的是实现工厂模式(你可以用谷歌搜索)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2014-08-11
    • 1970-01-01
    相关资源
    最近更新 更多