是否有办法使类名作为一个变量?.. Parent_class *p_derived = new classnameher



我试图遵循DRY,我想把一个新类的初始化在函数中,但有一个问题。我的类是一个父类,我在这个函数中初始化了一个新的派生类。

样本:

类声明:

#include <iostream>
class parent
{
private:
int m_number;
public:
parent() {}
virtual void say_hello() { std::cout << "Hello. This is parent class!" << std::endl; }
~parent() {}
};
class derived1 : public parent
{
public:
derived1(){}
void say_hello() override { std::cout << "Hello. This is first derived class!" << std::endl; }
~derived1(){}
};
class derived2 : public parent
{
public:
derived2() {}
void say_hello() override { std::cout << "Hello. This is second derived class!" << std::endl; }
~derived2() {}
};

Enum状态:

enum OutputMethod
{
STATE_NONE,
STATE_CLASS,
STATE_EXIT
};

测试动作:

#include <Windows.h>
#define VK_L_C 0x43
#define VK_L_N 0x4E
void action(OutputMethod &state)
{
if (GetKeyState(VK_L_C) < 0)
{
state = STATE_CLASS;
}
else if (GetKeyState(VK_L_N) < 0)
{
state = STATE_NONE;
}
else if (GetKeyState(VK_ESCAPE) < 0)
{
state = STATE_EXIT;
}
}

主:

#include <iostream>
#include <string>
int main(void)
{
derived1 *p_derived = nullptr;
OutputMethod out_method;
out_method = STATE_NONE;
while (out_method != STATE_EXIT)
{
// Actions (C-class, N-none, ESC-endloop)
action(out_method);
//--------BEGINING of desired function----------
if (out_method == STATE_NONE)
{
std::cout << "Hello from main!" << std::endl;
}
else if (out_method == STATE_CLASS)
{
if (p_derived == nullptr)
{
// There is the problem (derived1 name should be variable)
p_derived = new derived1();
}
p_derived->say_hello();
}
if (out_method == STATE_NONE)
{
if (p_derived != nullptr)
{
delete p_derived;
p_derived = nullptr;
}
}
//-------------END of desired function----------
}
if (p_derived != nullptr)
{
delete p_derived;
p_derived = nullptr;
}
std::string exit = "";
std::getline(std::cin, exit);
return 0;
}

我不知道该怎么做。

我已经试着做了一个函数来处理这个问题:

void output_state(parent *p_variable, parent* p_new, OutputMethod state_0, OutputMethod state_1, OutputMethod &state)
{
if (state == state_0)
{
std::cout << "Hello from main!" << std::endl;
}
else if (state == state_1)
{
if (p_variable == nullptr)
{
// There is the problem (p_new is not a name var but a new object init)
p_variable = p_new;
}
p_variable->say_hello();
}
if (state == state_0)
{
if (p_variable != nullptr)
{
delete p_variable;
p_variable = nullptr;
}
}
}

调用main:

...
derived1 *p_derived = nullptr;
OutputMethod out_method;
out_method = STATE_NONE;
while (out_method != STATE_EXIT)
{
// Actions (C-class, N-none, ESC-endloop)
action(out_method);
output_state(p_derived, new derived1(), STATE_NONE, STATE_CLASS, out_method);
}
...

但问题是,我不想初始化一个新对象,每次我调用函数,我不确定如何使检查能够调用函数,但同时防止初始化一个新对象。

我错过了什么吗?

p。S:很抱歉代码太长,但是当我发布一个简短的代码时,我得到的回复是人们无法读懂我的想法,但是如果我发布所有必要的部分,我得到的回复是代码太长,不可读。

您似乎希望您的函数生成给定类型的对象。template可以这样做:

template<class ObjType>
OutputMethod  output_state( parent *p_variable, OutputMethod state_0, OutputMethod state_1, OutputMethod state ) {
if ( state == state_0 ) {
std::cout << "Hello from main!" << std::endl;
}
else if ( state == state_1 ) {
if ( ! p_variable ) {
p_variable = new ObjType{};
}
p_variable->say_hello();
}
if ( state == state_0 ) {
if ( p_variable ) {
delete p_variable;
p_variable = nullptr;
}
}
return  state;
}

main:

while ( out_method != STATE_EXIT ) {
// Actions (C-class, N-none, ESC-endloop)
action(out_method);
out_method = output_state<Derived1>( p_derived, STATE_NONE, STATE_CLASS, out_method );
}

注意,我把p_variable作为原始指针,但使用std::unique_ptr<>更安全。

最新更新