C++ 无法将 base class "this" 指针分配给 base class 构造函数中的派生 class 对象
C++ Unable to assign base class "this" pointer the derived class object inside base class constructor
您好,我正在学习 C++,并且正在尝试构造一些东西,其中使用以下构造将 base class *this 指针分配给派生的 class 对象。我的问题是,这种方式在 C++ 中是否可行?因为,当我尝试编译它时,出现错误
错误:“Derived_1”之前需要类型说明符
如果这样的事情是可能的,那么正确的方法是什么。我的 objective 这样做是通过基于 DISPLAY 类型的基础 class 自动获取相对派生的 class 实现,以避免 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.
你绝对不能做你正在做的事。你不能这样做:
*this = new Derived_1();
我什至不想去想这其中的所有错误。相反,您想要的是实现工厂模式(您可以 google 实现)。
您好,我正在学习 C++,并且正在尝试构造一些东西,其中使用以下构造将 base class *this 指针分配给派生的 class 对象。我的问题是,这种方式在 C++ 中是否可行?因为,当我尝试编译它时,出现错误
错误:“Derived_1”之前需要类型说明符
如果这样的事情是可能的,那么正确的方法是什么。我的 objective 这样做是通过基于 DISPLAY 类型的基础 class 自动获取相对派生的 class 实现,以避免 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.
你绝对不能做你正在做的事。你不能这样做:
*this = new Derived_1();
我什至不想去想这其中的所有错误。相反,您想要的是实现工厂模式(您可以 google 实现)。