我是c ++的新手,我有一个问题.
假设我们有一个基类Base和两个派生类Derived1和Derived2.fe Derived1有一个带整数的构造函数,Derived2有一个带布尔值的构造函数.
是否有可能在运行时(或在编译时)确定要创建这两个子类中的哪一个并将其分配给Base类.
像这样:Base b = ???(value),其中value是integer或boolean类型.
提前致谢!
我知道C - char,int,float等基本数据类型.但C语言中究竟是什么派生数据类型?
LYAH在派生实例中说"所有的值构造函数都是无效的(不带参数,即字段),我们可以将它作为Enum类型类的一部分."
数据日=星期一| 星期二| 星期三| 星期四| 星期五| 星期六| 星期日
派生(Eq,Ord,Show,Read,Bounded,Enum)
现在,如果我需要几个月,它就会
数据月= 1月| 二月| 三月| 四月| 五月| 六月| 七月| 八月|九月| 十月| 11月| 12月
推导(Eq,Ord,Show,Read,Bounded,Enum)
我的问题是:(1)每月存储最大天数值的位置?(2)如果是闰年,如何提及和控制2月份,那么2月份的maxDays为29天,否则为28天?
在Java中,可以像下面给出的代码:
公共枚举月份{
1月(31),
2月(29日),
3月(31日),
4月(30日),
5月(31日),
6月(30日),
7月(31日),
8月(31日),
9月(30日),
10月(31),
十一月(30),
十二月(31)
,;
private int maxDays; //实例变量
private(int maxDays){//构造函数总是私有
this.maxDays = maxDays;
}
公众诠释getMaxDays(){
返回maxDays;
}
我有三个班:
class A
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & this->id & this->somefield;
}
protected:
A() {} // just for boost, normally I create this class with non-default constructor
int id;
Sometype somefield;
public:
A(unsigned int id);
// blah blah, also some virtual methods
};
// _____________________ CLASS B
class B : public A
{
private:
friend class boost::serialization::access;
template<class Archive> inline friend void load_construct_data(Archive &ar, B *t, const …Run Code Online (Sandbox Code Playgroud) 为了简化使用特定类型的字典,我从通用Dictionary <>派生了一个类来处理从公共基类派生的各种元素:
//my base class holding a value
public abstract class A{ public int aValue; }
//derived classes that actually are stuffed into the dictionary
public class B : A {...}
public class C : A {...}
//wrapper class for dictionary
public class MyDict : Dictionary<string, A>;
//my class using the dictionary
public class MyClass {
public MyDict dict = new MyDict();//use an instance of MyDict
public MyClass() { ... //fill dict with instances of B and C }
//function …Run Code Online (Sandbox Code Playgroud) 我需要帮助编写满足此条件的派生列表达式。
IF([职位名称] 是“埃森哲领导层”或“高级管理人员”)AND [薪资组] IS NOT NULL THEN [薪资组] ELSE [职位名称]。
谢谢,
假设我有两个派生自同一基类的类.我想根据命令行输入实例化一个新的类对象.我能做到这一点:
#include <iostream>
#include "DerivedClass1.h"
#include "DerivedClass2.h"
using namespace std;
int main(int argc, char *argv[])
{
if (argv[1] == "DERIVED CLASS 1") {
DerivedClass1 *myClass = new DerivedClass1(argv[2]);
myClass->doSomething();
} else if (argv[1] == "DERIVED CLASS 2") {
DerivedClass2 *myClass = new DerivedClass2(argv[2]);
myClass->doSomething();
}
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有更优雅的方式来做到这一点.我正在考虑创建一个抽象类工厂,或硬编码将字符串名称映射到类实例.几个限制
1)我的基类是抽象的 - 它包含纯虚函数
2)我只能调用参数化构造函数
我有以下计划:
#include<iostream>
using namespace std;
struct Base01{
int m;
Base01():m(2){}
void p(){cout<<m<<endl;}
};
struct Derived01:public Base01{
Derived01():m(3){}
};
struct Derived02:virtual public Base01{
Derived01():m(4){}
};
struct my: Derived01,Derived02{
my():m(5){}
};
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
gcc/clang都报告编译错误.
我只想知道这里的语言设计考虑是什么,为什么派生类只能在初始化列表中调用基类ctor,但不能直接使用基类成员?
我有以下代码:
class A
{
public:
int foo = 0;
};
class B: public A
{
public:
int foo = 1;
};
int main()
{
A *a = new B();
std::cout << a->foo;
std::getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:0
为什么 B 不覆盖 A 中的成员 foo ?
以及如何在不强制转换为派生类的情况下获得所需的 1 输出
我希望只有在类是特定的派生类时才能做某事.那就是我:
class X{
int id;
}
class A: public X{
void run();
}
class B: public X{
int lala;
}
Run Code Online (Sandbox Code Playgroud)
我想做一些事情:
main(){
vector<X *> types;
types.push_back(new A);
types.push_back(new B);
int var = 0;
for(int i = 0; i<types.size(); i++){
if(types[i].isType(A)) {types[i].run();}
}
for(int i = 0; i<types.size(); i++){
if(types[i].isType(B)) {var = lala;}
}
}
Run Code Online (Sandbox Code Playgroud)
我不希望B类有任何等同于run()的东西,也不希望A类具有等同于lala的东西.
我知道fortran有一个解决方法
select type ( x => var )
class is ( A )
x.run()
end select
Run Code Online (Sandbox Code Playgroud)
但我不确定我在C++中的选择是什么.
谢谢