Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can we use the name of the class instead of its object in the following syntax :
C++
student s;
fin.read((char*)&s,sizeof(s));

where s is the object of the class.
and student is the mane of the class

What I have tried:

Basically nothing,
Posted
Updated 4-Feb-18 23:39pm
v2
Comments
Jochen Arndt 5-Feb-18 6:01am    
You already got an answer to your question but you should be careful when reading and writing class contents as they are. That won't work if your class contains dynamic objects (pointers to allocated objects).
Richard MacCutchan 5-Feb-18 7:45am    
Why not just try it, ans see what results you get?

1 solution

Yes you can.
The compiler happily accetps
C++
fin.read((char*)&s,sizeof(student));
As you may easily verify.


The form
C++
fin.read((char*)&s,sizeof(s));
however is more robust (if you change the type of s it remains correct).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900