如何用新的类对象替换cin和cout [英] How to replace cin and cout with new class objects

查看:200
本文介绍了如何用新的类对象替换cin和cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何定义自己的类,我的对象可以用来在c ++中输入和输出而不是使用cin和cout。?



什么我试过了:



i还没试过,因为我不是这方面的专家

how can i define my own class whose object i can use to take input and output in c++ instead of using cin and cout.?

What I have tried:

i havent tried as i am not an expert at this

推荐答案

为什么(新鲜的地狱)你想要替换 cin cout

据我所知,处理控制台 I / O 的唯一(便携式)方法只是 iostream s或plain C stdio 。换句话说,您必须在自己的对象中包装 cin / cout (或 scanf / printf )? />
这是你真正想要的吗?或者您需要为您的类定义自定义提取/插入运算符?
Why (the fresh hell) do you want to replace cin and cout?
As far as I know the only (portable) ways to handle console I/O are just iostreams or plain C stdio. In other words you would have to wrap cin/cout (or scanf/printf) in your own objects?
Is this what you really want? Or you need instead to define custom extraction/insertion operators for your classes?


可能您不希望在实践中:cin和cout非常灵活,可以返回或接受大量的依赖于上下文的类型:整数,浮点数,双精度数,字符数,字符串...

你必须创建你的类,以便它可以返回每个类,这是很多为实际上不会使你的代码更具可读性的东西工作(并且当你的课程从控制台应用程序转移到没有使用cin和cout的GUI时,你将不会再使用几周)。



如果是我,我会找到一个更好的项目!
Probably, you don't want to in practice: cin and cout are very flexible, and can return or accept a huge number of context dependant types: integers, floats, doubles, chars, strings ...
You would have to make your class so that it could return each of those as well, and that's a lot of work for something that won't actually make your code any more readable (and that you won't use again in a couple of weeks when your course moves from console apps to GUI ones where cin and cout aren't used at all).

I'd find a better project if it was me!


这篇关于如何用新的类对象替换cin和cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆