some_type& function()

what's the meaning ostream& in something like this:

ostream& write (char* s, streamsize n);
// if i'm not mistaken bout the func declarations...

is it the same with just ostream? please explain this to me...
Last edited on
ostream write(...) would return a copy, while ostream& write(...) returns a reference (which is basically a pointer).
but why it said that the return value is: *this
as far as i know, this is a pointer, which means an address/ reference. while *this means the value and not the reference...
While a reference works like a pointer, it doesn't look like a pointer. It is meant to save you from all the &, * and ->. Example:
1
2
3
Object a;
Object* ptr = &a;
ptr->foo();
could be written as
1
2
3
Object a;
Object& ref = a;
ref.foo();
Find a tutorial on references if you want to know more (this site doesn't have one).
but how if it's a function? a function return a value, if it return value like *this then it's not a reference right? *this is a value from the address that referenced by it right?
closed account (1vRz3TCk)
First things first a reference is not a pointer, it is the object by a different name.

So, this is a self referencing pointer[1], it points to the object the holds the pointer. When you deference the this pointer (*this) you get the object that the pointer points to. The object is then returned. Try not to get hung-up on how the underling implementation actually carries this out, just think that a reference (&) is the object, not a copy of or a point to an object.

_______________________
[1] referencing as in the natural language meaning not a C++ reference type.
Last edited on
@codemonkey: ok thanks man. i think i got it. so to make it clear (as an enclosure) this:

1
2
3
4
ostream new_ostream ("open.txt");

//this function
new_ostream.write(some_char, buffer);

will return new_ostream itself to itself... right?
closed account (1vRz3TCk)
will return new_ostream itself to itself... right?
Hmmm...it doesn't return itself to it self.

I don't know if this will make it clearer or not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

struct Foo
{
    Foo & SayHello(){std::cout << "Hello "; return *this;}
    Foo & SayWorld(){std::cout << "World!"; return *this;}
};
 
int main ()
{
    Foo bar;

    bar.SayHello().SayWorld();
    
    return 0;
}


In the above, when bar.SayHello() is called the object returned (bar) will replace bar.SayHello() meaning that the next call will be bar.SayWorld().

hmm... i understand this part, but by the way, is this way is legal? i just did know... i guess i know it now (about this ampersand complicated stuff). unless there's anything you would like to say... thanks man...
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

struct T {
	int z;
	T() : z(0) {}
	T setC(int z) { this->z = z; return *this; }
	T& setR(int z) { this->z = z; return *this; }
} t, v, x;

int main() {
	T &u = t.setC(1).setC(200);

	T &w = v.setR(1).setR(200);
	w.setC(-3);

	T &y = x.setR(1).setC(200);
	y.setC(-3);

	cout << "t = " << t.z << endl
	     << "u = " << u.z << endl
	     << "v = " << v.z << endl
	     << "w = " << w.z << endl
	     << "x = " << x.z << endl
	     << "y = " << y.z << endl;
	return 0;
}
t = 1
u = 200
v = -3
w = -3
x = 200
y = -3
i got it. so setC only make the T becomes an independent variable because it return only copy of the other T variable. i'm a little bit confused at first, but this is a good example man...
Last edited on
Right, setC returns a copied T, which in turn copies it's member variable z (default behavior.) Where as, setR returns a reference, which will return the object (the actual object, not a copy) pointed to by this.
Topic archived. No new replies allowed.