Anonymous
Anonymous
Calebe94
Merry Christmas!
Francisco
y la
Luis José
Merry Xmas boys!
S.
Merry Xmas girls ( if any
Liam
Merry Xmas girls ( if any
Of course we have~
S.
Of course we have~
That's ... unexpected🤣
Liam
That's ... unexpected🤣
Well, in another word, beyond expection.
Ritu Raj
Why do we use reference of ostream/istream in operator overloading of insertion and extraction operator?
Ritu Raj
Instead of using reference can't we have used return type ostream/istream directly?
S.
Why do we use reference of ostream/istream in operator overloading of insertion and extraction operator?
I know little about C++. But I think rather than copying a whole "entity" of ostream/istream, it'd be much better to just copy and pass a "pointer"
S.
... which could be an implementation for "reference"
Ritu Raj
One by reference instead of assignment!
SAHDEV
Heyy
S.
I think We are using whole entity in both the cases!!
No. If you don't use reference, a new copy of the former osteam/istream "entity" will be created ... it costs time & memory
S.
but for a "reference", you can consider, only 4 or 8 bytes will be copied
S.
basically, using the overloaded operator is the same as calling a function
S.
you need to copy & pass some *value*s to the function
S.
For a pointer/reference, only 4 or 8 bytes; for a struct/class, bulk of memory. ( Assuming the compiler doesn't optimize anything
Ritu Raj
Now I understand
Ritu Raj
So we can use ostream and istream??
Ritu Raj
Instead of reference?
S.
Yes, with a lower performance
SAHDEV
Hey guys i hav no idea about swtich case...
SAHDEV
Plz tell me...
SAHDEV
How to use it
SAHDEV
And programe
Anonymous
S.
Hey guys i hav no idea about swtich case...
So, which part of switch...case?
Talula
Merry Christmas everyone...
Liam
Instead of using reference can't we have used return type ostream/istream directly?
No you can not, since both objects of istream and ostream are non-copyable, and when functions return, they copy local variable to their callers.
Liam
If you do so, compiler will raise an error telling you your codes is calling their copy constructors which are marked as deleted or private.
BinaryByter
fucking hell
Liam
There is another reason why we always return a reference to streams. Since insertion and extraction opreators are actually functions, calling something like: std::cout << "hello " << "world\n"; equals to operator<<(operator<<(std::cout, "hello"), "world\n") As you can see, the return of first calling will then be treated as input paramter of the second calling. If this kind of functions/operators do not return reference to streams, this kind of calling will then be invalid.
BinaryByter
Can we write a copy constructor to make it copyable?
no, because they are declared as deleted
Liam
Can we write a copy constructor to make it copyable?
Well, if you are trying to write a stream base class for yourself, yes; otherwise, no.
S.
Thank you both 😂
Liam
Also, you have to remind that, this kind of non-copyable objects imply some non-copyable machine resources. For example, sockets connection... So, copy this kind of objects is meanless and will cause errors.
BinaryByter
maybe move semantics work in your case
Liam
i fear this notation
lol, just for showing something. nobody will use it in real codes.
Liam
maybe move semantics work in your case
Move constructor and assignment are protected, so...
S.
Hi guys. I'm just familiar with C and start to learn C++. What book should I read to understand the implementation details like istream/ostream?
BinaryByter
lol dafaq
Liam
move constructor too?
yes, try to compile #include <iostream> std::ostream foo(std::ostream& os) { return std::move(os); } int main() { foo(std::cout); return 0; } gives you error: std::basic_ostream<_CharT, _Traits>::basic_ostream(std::basic_ostream<_CharT, _Traits>&&) [with _CharT = char; _Traits = std::char_traits<char>]’ is protected within this context return std::move(os); ^
Liam
faint, your turn..
BinaryByter
faint, your turn..
i'm drowning in oil
Liam
Hi guys. I'm just familiar with C and start to learn C++. What book should I read to understand the implementation details like istream/ostream?
If you're new to C++, I would not recommend you to read impl details like istream/ostream. It's so complecated with template tricks. Read introduction books first, and then dive into STL codes, if you like.
Ritu Raj
That means we can't transfer copy of cout where it is called,instead we can transfer only it's reference?
BinaryByter
yes
BinaryByter
why would you want to transfer a copy of cout?
Ritu Raj
why would you want to transfer a copy of cout?
Just to curious why we are using reference instead of using directly return type istream/ostream
BinaryByter
well what would a copy of cout mean?
BinaryByter
The standard couldn't answer that, thus they made the copy constructor deleted
Ritu Raj
Actually I am still studying c++😅😅.... That's why I have too much confusion
Liam
look, you have to notice that, there exists something that is not copyable. def foo(father): // do something, such as hair cut return father If the return type is Father, it means that after hair cut, you got two fathers. That's redicilous, isn't it?
BinaryByter
nobody would want that
BinaryByter
double scolding
BinaryByter
buuuuttt
BinaryByter
double income 🤔
Ritu Raj
double scolding
😂😂😂
Liam
s/red/rid
Well, I'm gonna get used to my poor English.