significance of 'this'

What is the significance of 'this' in :

file.write((char*)&this,sizeof(class));
Last edited on
A compile-time error; 'this' is not an lvalue
If you had the following code:

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
29
30
31
32
33
34
35
36
37
38
class Foo;

class Fee
{
public:
	Fee(){}
	~Fee(){}

	void f(Foo* foo)
	{
		foo->func2();
	}
};

class Foo
{
public:
	Foo(){}
	~Foo(){}

	void func1()
	{
		Fee fee;
		fee.f(this);
	}

	void func2()
	{

	}
};

int main()
{	
	Foo foo;
	foo.func1();
	return 0;
}


As you can see you have instantiated an instance of Foo in your main() function and have called func1() on the Foo instance, which in turn creates an instance of Fee calling f(), and passing a pointer to the calling Foo instance thus allowing the instance of Fee to call a public functions on Foo. I know this looks prety pointless but somtimes you want a class instance to be able to call back into its calling parent and by passing the pointer from 'this' aka self (in Visual Basic parlance) you are then able to do that...

Hope that makes any sense.

Andrew
So, getting back to your function:

 
file.write((char*)&this,sizeof(class));


You are passing a pointer from the current instance to your file.write(...) function which is therefore able to get access to public functions, getters and setters. So, you are probably in a document of some sort and wanting to write the document out to a file, by passing a pointer to this the file instance can get access to the data it is to write out to file... plus name of file etc... Quite why you would be forcing this to a char* I don't know without knowing further info
Last edited on
because the write function requires a char* - when writing in binary mode (usually binary) you pass a char* and the size of the thing writing to file (or fstream) - i would suggest only doing this type of write with simple built types - for your own types write your own serialize/deserialize functions or you will have many issues
As JLBorges said the compiler shall issue an error because this is prvalue while the operator & requires lvalue.

Syntaxically correct code will look like


file.write( ( const char* )this, sizeof( class ) );
Last edited on
Is this correct?
file.write( reinterpret_cast<const char*>(this), sizeof(this) );
Nope:
This is correct:
file.write( reinterpret_cast<const char*>(this), sizeof(*this) );

Remember 'this' is a pointer.
@majidkamali1370
Is this correct?
file.write( reinterpret_cast<const char*>(this), sizeof(this) );


It is in fact the same as was written above by me.

EDIT: Except as correctly pointed out EssGeEich that instead of sizeof( this ) shall be sizeof( class ) where class is the name of this particular class.
Last edited on
Just to clarify because I don't see it written anywhere above, if you're after the general meaning of the this keyword in a C++ class, it is just a pointer to itself.

Thanks guys
Topic archived. No new replies allowed.