创建从输入流中读取的对象。 [英] Creating an object that is read from an input stream.
问题描述
我想允许从输入流中读取我的类的对象。我在执行方面遇到了麻烦。
以下是我尝试过的不同方法:
//版本1.0 - 默认构造函数
类MyClass
{
Foo foo; // foo和bar需要默认构造函数
条形栏;
public:
// MyClass的默认构造函数
MyClass()// foo和bar默认初始化
{
}
std :: istream& extract( std :: istream& is)
{
if(!(is>> foo))
return is;
if(!(是>> bar))
返回是;
返回是;
} < br $>
};
std :: istream& operator>>(std :: istream& is,MyClass& object)
{
返回>>对象;
}
//版本2.0 - std :: istream构造函数参数,默认构造函数和
例外>
class MyClass
{
Foo foo; // foo和bar需要默认构造函数
条形栏;
public:
MyClass(std :: istream& is)// foo和栏默认初始化
{
如果(!(是>> foo))
抛出MyException;
if(!(是>> bar))
抛出MyException;
}
};
//版本3.0 - 共享指针
class MyClass
{
Foo foo;
酒吧;
公开:
MyClass(Foo foo_,Bar bar_):foo(foo_),bar(bar_){}
};
std :: istream& operator>>(std :: istream& is,SharedPtr< MyClass>& object_ptr)
{
SharedPtr< Foo> foo_ptr;
if(!(是>>> foo_ptr))
返回是;
Foo foo = * foo_ptr;
SharedPtr< Bar> bar_ptr;
if(!(是>>> bar_ptr))
返回是;
Bar bar = * bar_ptr;
object_ptr = new MyClass(foo,bar);
返回;
}
//版本4.0 - 静态读取方法和例外
class MyClass
{
Foo foo
酒吧栏;
公开:
MyClass(Foo foo_,Bar bar_):foo(foo_),bar(bar_){}
static MyClass read(std :: istream& is);
};
MyClass MyClass :: read(std :: istream& is)
{
试试{
Foo foo = Foo :: read(is);
Bar bar = Bar :: read(is);
返回MyClass(foo,bar);
} catch(FooException e){
抛出MyException(e .get_desc());
} catch(BarException e){
抛出MyException(e.get_desc());
}
}
第三个版本是唯一不涉及默认值的方法
构造函数o例外。我怎样才能编写我的类来允许它从流中读取
对象?谢谢。
I want to allow objects of my class to be read from an input stream. I am
having trouble with the implementation. Here are the different approaches I
have tried:
// Version 1.0 - Default constructors
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
// default constructor for MyClass
MyClass() // foo and bar are default initialised
{
}
std::istream &extract(std::istream &is)
{
if (!(is >> foo))
return is;
if (!(is >> bar))
return is;
return is;
}
};
std::istream &operator>>(std::istream &is, MyClass &object)
{
return is >> object;
}
// Version 2.0 - std::istream constructor argument, default constructors and
exceptions
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
MyClass(std::istream &is) // foo and bar are default initialised
{
if (!(is >> foo))
throw MyException;
if (!(is >> bar))
throw MyException;
}
};
// Version 3.0 - Shared pointers
class MyClass
{
Foo foo;
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
};
std::istream &operator>>(std::istream &is, SharedPtr<MyClass> &object_ptr)
{
SharedPtr<Foo> foo_ptr;
if (!(is >> foo_ptr))
return is;
Foo foo = *foo_ptr;
SharedPtr<Bar> bar_ptr;
if (!(is >> bar_ptr))
return is;
Bar bar = *bar_ptr;
object_ptr = new MyClass(foo, bar);
return is;
}
// Version 4.0 - Static read methods and exceptions
class MyClass
{
Foo foo
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
static MyClass read(std::istream &is);
};
MyClass MyClass::read(std::istream &is)
{
try {
Foo foo = Foo::read(is);
Bar bar = Bar::read(is);
return MyClass(foo, bar);
} catch (FooException e) {
throw MyException(e.get_desc());
} catch (BarException e) {
throw MyException(e.get_desc());
}
}
The third version is the only approach that does not involve default
constructors or exceptions. How else can I write my class to allow it''s
objects to be read from a stream? Thanks.
推荐答案
我错误地写了2.0版。以下是我真正想写的内容:
//版本2.0 - std :: istream构造函数参数和异常
class MyClass
{
Foo foo;
酒吧酒吧;
公开:
MyClass(std :: istream& ;是):foo(是),bar(是)
{
}
};
这使版本4.0变得多余。
I wrote version 2.0 wrong. Here is what I really meant to write:
// Version 2.0 - std::istream constructor argument and exceptions
class MyClass
{
Foo foo;
Bar bar;
public:
MyClass(std::istream &is) : foo(is), bar(is)
{
}
};
This makes version 4.0 redundant.
" Jason Heyes" < GE ****** @ optusnet.com.au>在留言中写道
新闻:3f ********************** @ news.optusnet.com.au ...
"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f**********************@news.optusnet.com.au ...
我想允许从输入流中读取我的类的对象。我在执行方面遇到了麻烦。以下是我试过的不同方法
:
//版本1.0 - 默认构造函数
类MyClass
{F / foo; // foo和bar需要默认构造函数
栏栏;
public:
// MyClass的默认构造函数
MyClass()// foo和bar默认初始化
{
}
std :: istream& extract(std :: istream& is)
{
if(!(是>> foo) )
返回是;
if(!(是>> bar))
返回是;
返回是;
}
};
std :: istream& operator>>(std :: istream& is,MyClass& object)
{
返回>>宾语;
此函数执行无限递归调用。你的意思是
object.extract(是)?
}
为什么不让这个函数成为班级的朋友,并让它做
类提取函数所做的事情,并摆脱提取函数?为什么在你拿一个时需要两个步骤?
//版本2.0 - std :: istream构造函数参数,默认构造函数
和异常
类MyClass
{F / foo; // foo和bar需要默认构造函数
条形栏;
public:
MyClass(std :: istream& is)// foo和bar默认初始化
{
if(!(是>> foo))
抛出MyException;
if(!(是>> bar))
抛出MyException;
}
};
有了这个,你就无法读入现有的物体。
//版本3.0 - 共享指针
类MyClass
{
Foo foo;
酒吧吧;
公开:
MyClass(Foo foo_,Bar bar_):foo(foo_),bar(bar_){}
};
std :: istream& operator>>(std :: istream& is,SharedPtr< MyClass>& object_ptr)
{
SharedPtr<富> foo_ptr;
if(!(是>>> foo_ptr))
返回是;
Foo foo = * foo_ptr;
SharedPtr< Bar> bar_ptr;
if(!(是>>> bar_ptr))
返回是;
Bar bar = * bar_ptr;
object_ptr = new MyClass(foo,吧);
返回是;
}
所有这些看起来都很麻烦。
//版本4.0 - 静态读取方法和异常
类MyClass
{Foo foo
酒吧;
公开:
MyClass(Foo foo_,Bar bar_) :foo(foo_),bar(bar_){}
静态MyClass读取(std :: istream& is);
};
MyClass MyClass: :read(std :: istream& is)
{
尝试{
Foo foo = Foo :: read(is);
Bar bar = Bar :: read(是);
返回MyClass(foo,bar);
} catch(FooException e){
抛出MyException(e.get_desc());
} catch(BarException e){
抛出MyException(e.get_desc());
}
}
I want to allow objects of my class to be read from an input stream. I am
having trouble with the implementation. Here are the different approaches I have tried:
// Version 1.0 - Default constructors
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
// default constructor for MyClass
MyClass() // foo and bar are default initialised
{
}
std::istream &extract(std::istream &is)
{
if (!(is >> foo))
return is;
if (!(is >> bar))
return is;
return is;
}
};
std::istream &operator>>(std::istream &is, MyClass &object)
{
return is >> object;
This function does an infinite recursive call. Did you mean
object.extract(is)?
}
Why not make this function a friend of the class, and let it do what the
class extract function does, and get rid of the extract function? Why take
two steps when you can take one?
// Version 2.0 - std::istream constructor argument, default constructors and exceptions
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
MyClass(std::istream &is) // foo and bar are default initialised
{
if (!(is >> foo))
throw MyException;
if (!(is >> bar))
throw MyException;
}
};
With this, you can''t read into an existing object.
// Version 3.0 - Shared pointers
class MyClass
{
Foo foo;
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
};
std::istream &operator>>(std::istream &is, SharedPtr<MyClass> &object_ptr)
{
SharedPtr<Foo> foo_ptr;
if (!(is >> foo_ptr))
return is;
Foo foo = *foo_ptr;
SharedPtr<Bar> bar_ptr;
if (!(is >> bar_ptr))
return is;
Bar bar = *bar_ptr;
object_ptr = new MyClass(foo, bar);
return is;
}
All this looks like a lot of trouble.
// Version 4.0 - Static read methods and exceptions
class MyClass
{
Foo foo
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
static MyClass read(std::istream &is);
};
MyClass MyClass::read(std::istream &is)
{
try {
Foo foo = Foo::read(is);
Bar bar = Bar::read(is);
return MyClass(foo, bar);
} catch (FooException e) {
throw MyException(e.get_desc());
} catch (BarException e) {
throw MyException(e.get_desc());
}
}
再次,你不能读到现有的对象。
/>
在所有这些中,版本1(经过适当修改)看起来对我来说是最好的,但也许
你有特殊的理由喜欢另一个。
DW
Again, you can''t read into an existing object.
Of all these, version 1 (suitably modified) looks the best to me, but maybe
you have special reasons for preferring another.
DW
" Jason Heyes" < GE ****** @ optusnet.com.au>在留言中写道
新闻:3f ********************** @ news.optusnet.com.au ...
"Jason Heyes" <ge******@optusnet.com.au> wrote in message
news:3f**********************@news.optusnet.com.au ...
我想允许从输入流中读取我的类的对象。我在执行方面遇到了麻烦。以下是我试过的不同方法
:
//版本1.0 - 默认构造函数
类MyClass
{F / foo; // foo和bar需要默认构造函数
栏栏;
public:
// MyClass的默认构造函数
MyClass()// foo和bar默认初始化
{
}
std :: istream& extract(std :: istream& is)
{
if(!(是>> foo) )
返回是;
if(!(是>> bar))
返回是;
返回是;
}
};
std :: istream& operator>>(std :: istream& is,MyClass& object)
{
返回>>对象;
//
//版本2.0 - std :: istream构造函数参数,默认构造函数
和异常
类MyClass
{
Foo foo; // foo和bar需要默认构造函数
条形栏;
public:
MyClass(std :: istream& is)// foo和bar默认初始化
{
if(!(是>> foo))
抛出MyException;
if(!(是>> bar))
抛出MyException;
}
};
//版本3.0 - 共享指针
类MyClass
{
Foo foo;
栏吧;
公开:
MyClass(Foo foo_,Bar bar_):foo(foo_),bar(bar_){}
};
std :: istream& operator>>( std :: istream& is,SharedPtr< MyClass>& object_ptr)
{
SharedPtr< Foo> foo_ptr;
if(!(是>>> foo_ptr))
返回是;
Foo foo = * foo_ptr;
SharedPtr< Bar> bar_ptr;
if(!(是>>> bar_ptr))
返回是;
Bar bar = * bar_ptr;
object_ptr = new MyClass(foo,吧);
返回;
}
//版本4.0 - 静态读取方法和异常
类MyClass
{F / foo foo foo
酒吧吧;
公开:
MyClass(Foo foo_,Bar bar_):foo(foo_),bar(bar_){}
静态MyClass读取(std) :: istream& is);
};
MyClass MyClass :: read(std :: istream& is)
{
尝试{
Foo foo = Foo :: read(is);
Bar bar = Bar :: read(is);
返回MyClass(foo,bar);
} catch(FooException e){
抛出MyException(e.get_desc());
} catch(BarException e){
抛出MyException(e.get_desc());
}
}
第三个版本是唯一不涉及默认构造函数或异常的方法。我怎样才能编写我的类以允许它从流中读取对象?谢谢。
I want to allow objects of my class to be read from an input stream. I am
having trouble with the implementation. Here are the different approaches I have tried:
// Version 1.0 - Default constructors
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
// default constructor for MyClass
MyClass() // foo and bar are default initialised
{
}
std::istream &extract(std::istream &is)
{
if (!(is >> foo))
return is;
if (!(is >> bar))
return is;
return is;
}
};
std::istream &operator>>(std::istream &is, MyClass &object)
{
return is >> object;
}
// Version 2.0 - std::istream constructor argument, default constructors and exceptions
class MyClass
{
Foo foo; // foo and bar require default constructors
Bar bar;
public:
MyClass(std::istream &is) // foo and bar are default initialised
{
if (!(is >> foo))
throw MyException;
if (!(is >> bar))
throw MyException;
}
};
// Version 3.0 - Shared pointers
class MyClass
{
Foo foo;
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
};
std::istream &operator>>(std::istream &is, SharedPtr<MyClass> &object_ptr)
{
SharedPtr<Foo> foo_ptr;
if (!(is >> foo_ptr))
return is;
Foo foo = *foo_ptr;
SharedPtr<Bar> bar_ptr;
if (!(is >> bar_ptr))
return is;
Bar bar = *bar_ptr;
object_ptr = new MyClass(foo, bar);
return is;
}
// Version 4.0 - Static read methods and exceptions
class MyClass
{
Foo foo
Bar bar;
public:
MyClass(Foo foo_, Bar bar_) : foo(foo_), bar(bar_) { }
static MyClass read(std::istream &is);
};
MyClass MyClass::read(std::istream &is)
{
try {
Foo foo = Foo::read(is);
Bar bar = Bar::read(is);
return MyClass(foo, bar);
} catch (FooException e) {
throw MyException(e.get_desc());
} catch (BarException e) {
throw MyException(e.get_desc());
}
}
The third version is the only approach that does not involve default
constructors or exceptions. How else can I write my class to allow it''s
objects to be read from a stream? Thanks.
当我在写软件时,我发现自己经常问,到底是什么?与B有什么关系?在你的情况下,MyClass与
流有什么关系?我知道我真的不知道MyClass是什么,但我会打赌
它与流没有任何关系。因此我不愿意将b $ b引入构造函数或内置函数。
显而易见的方法是使用我的想法(非标准) term)a延迟
构造函数:
class MyClass
{
private:
Foo m_foo;
Bar m_bar;
public:
MyClass(){}
void set(const Foo& i_foo,const Bar& i_bar){m_foo = i_foo; m_bar =
i_bar;}
...
};
初始化过程然后将使用此处未显示的软件读取临时foo和bar
,然后调用set。当然,如果Foo和Bar是大型物体,这可能是非常昂贵的。 (但是在你放弃之前做一些运行
时间研究!一个好的优化编译器有时可能会让你感到惊讶。)如果复制真的太贵了可能会有/>
替代品。例如,假设Foo实际上是一个可能很大的
std :: vector。然后我会有这样的成员函数:
模板< typename ITER>
set_foo(ITER优先,ITER最后){std :: copy(第一个,最后一个,
std :: back_inserter(m_foo);}
同样,没有特定的流引用,尽管可以轻松使用
stream使用istream_iterator<> ;.
如果所有这些都失败了,我会回到1.0版本。这似乎是一个实用的解决方案混合了MyClass和流的概念,但是
最少能有效地完成工作。
-
Cy > http://home.rochester.rr.com/cyhome/
这篇关于创建从输入流中读取的对象。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!