Coord(a)
似乎不起作用,lon
和lat
的值为00.0000
和00.0000
,默认值为in >> Coord(a)
和lon
构造函数。我该怎么办?
我的语法有问题吗?我不是lat
从基类读取 //base class
class Coord {
private:
double lon;
double lat;
Coord() { lon = 00.000000000000000; lat = 00.000000000000000; }
//.....
//.....
//.....
friend istream& operator>>(istream& in, Coord& temp)
{
cout << "Longitude is : "; in >> temp.lon;
cout << "Latitude is : "; in >> temp.lat;
return in;
}
};
//derived class
class Location : public Coord {
private:
char model[6];
double time;
int speed;
//.....
//.....
//.....
friend istream& operator>>(istream& in, Location& a)
{
cout << "Model is : "; in >> a.model;
cout << "Time is : "; in >> a.time;
cout << "Speed is : "; in >> a.speed;
cout << "Coordinates : " << endl; in >> Coord(a);
return in;
}
};
void main()
{
Location loc;
cin>>loc; cout<<loc;
}
和re.match(rex, s)
吗?
s
答案 0 :(得分:0)
正如我所说,Coord(a)
创建了一个副本(很像object slicing)。您的代码不应该编译,因为您将rvalue传递给operator>>
,后者需要左值引用。
您必须使用static_cast
来获取引用到Coord
基类:
in >> static_cast<Coord&>(a);
这将使其调用右operator>>
。