ostream

Inheriting and overriding ostream operator in C++

自作多情 提交于 2019-12-17 16:29:11
问题 I've been trying to find an answer to this, but no one seems to have exactly the same problem as I do. I am working with several derived classes. The ostream operator << for each of these should print out some things common to each, and some things specific to each. Later on, I would like to further derive from these derived classes, and again the new derived classes need to print out some things that are in the "generations" above them. For example: The Base class .h file class Base { int

Order of execution in operator <<

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:54:03
问题 I have difficulties in understanding the sequence of calls in the code below. I was expecting to see the output below A1B2 While I can see that the output I get is BA12 I thought that the call std::cout<< b->fooA() << b->fooB() << std::endl was equivalent to call std::cout.operator<<( b->fooA() ).operator<< ( b->fooB() ) but I can see that this is not the case. Can you help me understanding better how this does it work and the relationship with the global operator<< ? Is this last ever called

overloading friend operator<< for template class

南笙酒味 提交于 2019-12-16 19:31:10
问题 I have read couple of the questions regarding my problem on StackOverflow.com now, and none of it seems to solve my problem. Or I maybe have done it wrong... The overloaded << works if I make it into an inline function. But how do I make it work in my case? warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function warning: (if this is not what you intended, make sure the function template has already been declared and add <> after

Use of stringstream within ostream function

感情迁移 提交于 2019-12-14 00:22:09
问题 I'm looking into providing ostream operators for some math classes (matrix, vector, etc.) A friend has noted that the gcc standard library implementation of the ostream operator for std::complex includes the internal use of a string stream to format the output before passing it to the actual ostream : /// Insertion operator for complex values. template<typename _Tp, typename _CharT, class _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<

Formatted output arithmetic inserters

泄露秘密 提交于 2019-12-13 21:52:24
问题 I have a basic question about the arithmetic inserters; § 27.7.3.6.2/1 [ostream.inserters.arithmetic]: When val is of type bool, long, unsigned long, long long, unsigned long long, double, long double, or const void*, the formatting conversion occurs as if it performed the following code fragment: bool failed = use_facet< num_put<charT,ostreambuf_iterator<charT,traits> > > (getloc()).put(*this, *this, fill(), val).failed() The question is what exact function performs the conversion from a

Taking ownership of streambuf/stringbuf data

半世苍凉 提交于 2019-12-13 20:06:20
问题 I'd like an interface for writing to an automatically resizing array. One way to do this is with a generic std::ostream * . Then consider if ostringstream is the target: void WritePNG(ostream *out, const uint8_t *pixels); void *WritePNGToMemory(uint8_t *pixels) { ostringstream out; WritePng(&out, pixels); uint8_t *copy = new uint8_t[out.tellp()]; memcpy(copy, out.str().c_str(), out.tellp()]; return copy; } But I want to avoid the memcpy(). Is there a way to take ownership of the array in the

std::num_put issue with nan-boxing due to auto-cast from float to double

断了今生、忘了曾经 提交于 2019-12-13 14:19:47
问题 I'm using this post to extend nan values with some extra info and this post to modify std::cout behaviour and display this extra info. Here is the code defining the functions and NumPut class: #include <iostream> #include <assert.h> #include <limits> #include <bitset> #include <cmath> #include <locale> #include <ostream> #include <sstream> template <typename T> void showValue( T val, const std::string& what ) { union uT { T d; unsigned long long u; }; uT ud; ud.d = val; std::bitset<sizeof(T)

ostream: class that outputs either on cout or on a file

限于喜欢 提交于 2019-12-11 19:04:49
问题 I need to write a program that output either to the std::cout or to some file. I was reading this post to see how to do. However I would like to separate the management of the ostream from the main . So I was thinking to write a class, but I'm a bit confused about the design. I have in mind two solution (publicly) Subclass ostream: it this way I would have all method of ostream. However here the main problem would be the creator: class sw_ostream : public ostream { sw_ostream (cost char*

Operator Overloading: Ostream/Istream

萝らか妹 提交于 2019-12-11 07:23:59
问题 I'm having a bit of trouble with a lab assignment for my C++ class. Basically, I'm trying to get the "cout << w3 << endl;" to work, so that when I run the program the console says "16". I've figured out that I need to use an ostream overload operation but I have no idea where to put it or how to use it, because my professor never spoke about it. Unfortunately, I HAVE to use the format "cout << w3" and not "cout << w3.num". The latter would be so much quicker and easier, I know, but that's not

Simple wostream logging class (with custom stream manipulators)

时光怂恿深爱的人放手 提交于 2019-12-10 18:07:56
问题 I've been reading tons of questions, articles, and documentation, but I've not found a solution to my problem. I'd like to create a simple class for use in debugging. The end result of which would allow me to do something like this: logger << error << L"This is a problem!" << endl; logger << warning << L"This might be a problem!" << endl; logger << info << L"This isn't a problem but I thought you should know about it" << endl; With the idea that within the logger class I can toggle whether or