How do Manipulators work?

http://www.cplusplus.com/reference/iostream/manipulators/oct/

If we do

ostringstream os;

lalu os << oct;

what is oct?

What is the type of oct?

Is << operator defined for os and oct?

Is oct a special character?

Why we don't call oct ()? why oct only?
http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/
notice the overloads
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));


these take a pointer to a function.
oct itself is declared as inline ios_base& oct(ios_base& _Iosbase){ /*...*/ }
google function pointers if you want to know more.
Last edited on
Ah I see

So oct is a pointer to a function. That's why we call that oct and not oct();

Hmm.... strange. Why do they do it that way?

I mean why not declare a class called manipulator and define

ostream& operator << manipulator;?

Ah let me guess, the changing of ostream is done within the function it self

Hmm....
that's how function pointers work in c++ (c)
func() is a function and func is a pointer to it.

well, then you'd have to have a bunch of global objects or construct them every time you use them. but there's nothing wrong with the idea.

and yes, changes are made in that function.
It would mean that every time a new manipulator was created, class ostream& would have to be augmented
with another overload for the new manipulator, which would effectively prevent anyone other than the
writers of the C++ I/O streams library from writing a custom manipulator.
huh? no.struct manipulator{ virtual ostream& manipulate(ostream&); }; then of course << would have to take manipulator*, but I assume teguh123 just made a mistake about that.
And even if you didn't use polymorphism, you could still overload the << operator.
Topic archived. No new replies allowed.