在我的程序中,我有几个不同的类,它们包含一个具有相同名称但输出不同的函数:
#include <iostream>
#define CLASS_TO_USE C
class A{
public:
std::string class_name = "Class A";
A()
{};
double add(const double a, const double b) const
{
return a + b;
}
};
class B{
public:
std::string class_name = "Class B";
B()
{};
double add(const double a, const double b) const
{
return 2 * a + b;
}
};
class C{
public:
std::string class_name = "Class C";
C()
{};
double add(const double a, const double b) const
{
return 3 * a + b;
}
};
template <class T>
void calculate_addition(const T &local_class, const double a, const double b)
{
std::cout << "Addition gives the value " << local_class.add(a, b) << '\n';
}
int main()
{
CLASS_TO_USE local_class;
std::cout << "Hello World!" << std::endl;
calculate_addition(local_class, 2, 3);
return 0;
}
当前,我通过更改CLASS_TO_USE
的变量在这些类之间切换,但是该方法需要重新编译,这需要一些时间。相反,我更喜欢一种在运行时有效的方法,即可以在其中使用f.ex的方法。用户的输入。有可能吗?如果可以,怎么办?
编辑:我使用以下代码解决了问题:
#include <iostream>
#include <variant>
class A{
public:
std::string class_name_string = "Class A";
A()
{};
virtual double add(const double a, const double b) const
{
(void) a;
(void) b;
return 0;
};
virtual std::string class_name(void) const
{
return class_name_string;
}
};
class B : public A{
public:
std::string class_name_string = "Class B";
B()
{};
double add(const double a, const double b) const
{
return 2 * a + b;
}
virtual std::string class_name(void) const
{
return class_name_string;
}
};
class C : public A{
public:
std::string class_name_string = "Class C";
C()
{};
double add(const double a, const double b) const
{
return 3 * a + b;
}
virtual std::string class_name(void) const
{
return class_name_string;
}
};
class D : public A{
public:
std::string class_name_string = "Class D";
D()
{};
double add(const double a, const double b) const
{
return a + b;
}
virtual std::string class_name(void) const
{
return class_name_string;
}
};
void calculate_base_addition(const A &local_class, const double a, const double b)
{
std::cout << "Addition gives the value " << local_class.add(a, b) << " by using " << local_class.class_name() << '\n';
}
int main()
{
A *base_class;
B tmp_class_B;
C tmp_class_C;
D tmp_class_D;
int class_to_choose = 2;
switch(class_to_choose)
{
case 0:
{
base_class = &tmp_class_B;
break;
}
case 1:
{
base_class = &tmp_class_C;
break;
}
case 2:
default:
{
base_class = &tmp_class_D;
break;
}
}
std::cout << "Name of base_class: " << (*base_class).class_name() << '\n';
std::cout << "Name of derived class: " << tmp_class_B.class_name() << '\n';
std::cout << "Hello World!" << std::endl;
calculate_base_addition(*base_class, 2, 3);
return 0;
}
,并在此处提出后续问题:Define class variable in class based on user input
答案 0 :(得分:1)
您可以分派整数用户输入,例如像这样:
(bCompleted) in
if (bCompleted) {
UIView.animate(withDuration: 1.5, animations: {
book1ImageViewI.layer.transform = CATransform3DRotate(transform, -.pi/2, 0, 1, 0)
book1ImageViewI.image = UIImage(named:"0a6752b7cd35fc441c1528ee5078384d--antique-books-rabbit-holer.png")
}, completion: {
(bFinished) in
//Whatever
})
}