#include <iostream>
#include <string>
using namespace std;
class predicate
{
public:
predicate(string a,string o,int v):attr(a),opera(o),value(v){};
public:
string attr;
string opera;
int value;
};
class Event:public predicate
{
public:
//Event(string a,string o,int v,string a1,string o1,int v1):predicate(a,o,v),pred[]{(a1,o1,v1)}{};
//Event(predicate pred[]){};
public:
predicate pred[10];
};
int main()
{
predicate pred[2]={predicate(“a”,”=”,1),predicate(“B”,”<“,2)};
Event event;
//Event event={(a,=,1),(B,<,2)};
cout<<event.pred[0].attr<<endl;
cout<<event.pred[1].attr<<endl;
}
首先本人想要构建一个event.pred[10]这样一个子对象数组,在类Event里面构造函数不知道怎么写,本人写的两行注释掉的好像都不对,本人想用event.pred[0].attr来获得attr。
#include <string>
using namespace std;
class predicate
{
public:
predicate(string a,string o,int v):attr(a),opera(o),value(v){};
public:
string attr;
string opera;
int value;
};
class Event:public predicate
{
public:
//Event(string a,string o,int v,string a1,string o1,int v1):predicate(a,o,v),pred[]{(a1,o1,v1)}{};
//Event(predicate pred[]){};
public:
predicate pred[10];
};
int main()
{
predicate pred[2]={predicate(“a”,”=”,1),predicate(“B”,”<“,2)};
Event event;
//Event event={(a,=,1),(B,<,2)};
cout<<event.pred[0].attr<<endl;
cout<<event.pred[1].attr<<endl;
}
首先本人想要构建一个event.pred[10]这样一个子对象数组,在类Event里面构造函数不知道怎么写,本人写的两行注释掉的好像都不对,本人想用event.pred[0].attr来获得attr。
解决方案
20
你可以把构造函数改为
Event(string a,string o,int v,string a1,string o1,int v1):predicate(a,o,v){
// Code for assigning predicate array pred[10]
};
Event(string a,string o,int v,string a1,string o1,int v1):predicate(a,o,v){
// Code for assigning predicate array pred[10]
};
40
40
没必要写构造函数,直接把Event搞成聚合类就行了:
#include <iostream> #include <string> using namespace std; class predicate { public: predicate(string a,string o,int v):attr(a),opera(o),value(v){}; public: string attr; string opera; int value; }; struct Event { predicate pred[2]; }; int main() { Event event{{predicate("a","=",1), predicate("B","<",2)}}; cout << event.pred[0].attr << endl; cout << event.pred[1].attr << endl; }
另外,从你的要求来看,完全没必要让Event去继承predicate。