返回信息流这个代码基本是c++编程思想里14章继承与组合,子类型设置的代码。
我加了一个getline却发现不能接受FName1 file,有bug
我不明白为啥FName1有类型转换函数,getline函数还是不能接受file对象呢,应该能够自动按照ifstream接收呀。
我试了print函数,里面有getline,接收的参数类型是ifstream&,是没有问题的。
本来以为编译器不接受函数参数多次类型转换,但自己试的代码也是没问题的(从一个类型利用operator转换到另一个类型,再转换为基类类型)
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class FName1 {
ifstream file;
string fileName;
bool named;
public:
FName1() :named(false) {}
FName1(const string& fname)
:fileName(fname), file(fname.c_str()) {
named = true;
}
string name() const {
return fileName;
}
void name(const string& newName) {
if (named)
return;
fileName = newName;
named = true;
}
operator ifstream&() {
return this->file;
}
// ifstream& ifstreamobject(){
// return file;
// }
void close() {
file.close();
}
};
void print(ifstream& in) {
string s;
while (getline(in,s))
{
cout << s << endl;
}
return;
}
int main() {
FName1 file("out.dat");
cout << "file.name()" << endl;
string s;
while (getline(file, s)) {
cout << s << endl;
}
print(file);
file.close();
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #90333同步于 2016/2/22
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
getline能否接受自动类型转换?
timruning
2016/2/22镜像同步14 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
在cppreference.com上getline函数类似于:
template< class CharT, class Traits, class Allocator >
std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
std::basic_string<CharT,Traits,Allocator>& str);
getline(file, s)好像是两步类型转换请求:FName1->ifstream&->basic_istream&,应该不行。
好像换成getline(static_cast<ifstream&>(file), s)就可以了。
【 在 d1264003247 的大作中提到: 】
: 在cppreference.com上getline函数类似于:
: template< class CharT, class Traits, class Allocator >
: std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
: ...................
需要一个强制类型转换?可是我自己写一个代码
base1->operator()->base2->base,函数是可以调用到base的呀
【 在 d1264003247 的大作中提到: 】
: 在cppreference.com上getline函数类似于:
: template< class CharT, class Traits, class Allocator >
: std::basic_istream<CharT,Traits>& getline( std::basic_istream<CharT,Traits>& input,
: ...................
其实我不明白的是已经有类型转换函数了,为啥还要强制类型转换。
不是啊,你只定义了ifstream&类型转换,但没定义getline函数所需要的std::basic_istream&类型转换。所以要么再定义一个类型转换,要么static_cast强制转换。我是小白,这些都是我想的,错了不要怪我。。
【 在 timruning 的大作中提到: 】
: 其实我不明白的是已经有类型转换函数了,为啥还要强制类型转换。
【 在 d1264003247 的大作中提到: 】
: 不是啊,你只定义了ifstream&类型转换,但没定义getline函数所需要的std::basic_istream&类型转换。所以要么再定义一个类型转换,要么static_cast强制转换。我是小白,这些都是我想的,错了不要怪我。。
我也不明白。
我想的是我定义了ifstream&类型转换,ifstream不应该是istream的派生类么
【 在 d1264003247 的大作中提到: 】
: 不是啊,你只定义了ifstream&类型转换,但没定义getline函数所需要的std::basic_istream&类型转换。所以要么再定义一个类型转换,要么static_cast强制转换。我是小白,这些都是我想的,错了不要怪我。。
比如说这个代码,不也是需要两次类型转换么
#include <iostream>
#include<fstream>
#include<string>
using namespace std;
class A{
int i;
public:
void print() const {
cout<<"hello world!\n";
}
};
class B:public A{
string b;
};
class C{
public:
operator B() const{
return B();
}
};
void printA(const A& a){
a.print();
cout<<"this is A class";
}
int main(){
C c;
printA(c);
}
这应该只是一步隐式转换C->B,然后引用绑定到B的基类部分,这个不是类型转换。
【 在 timruning 的大作中提到: 】
: 比如说这个代码,不也是需要两次类型转换么
: #include <iostream>
: #include<fstream>
: ...................
【 在 d1264003247 的大作中提到: 】
: 这应该只是一步隐式转换C->B,然后引用绑定到B的基类部分,这个不是类型转换。
哦,这样。
ifstream->basic_istream不一样么?
那个是引用转换成引用,应该是类型转换。感觉是。。
【 在 timruning 的大作中提到: 】
: 哦,这样。
: ifstream->basic_istream不一样么?