[Qt-interest] Accessing a class member from an operator

Andre Somers andre at familiesomers.nl
Fri Apr 23 09:32:20 CEST 2010


On 22-4-2010 20:15, Oliver Heins wrote:
> John Weeks<john at wavemetrics.com>  writes:
>
>    
>> On Apr 22, 2010, at 10:27 AM, Oliver Heins wrote:
>>
>>      
>>> I call the QDataStream&operator>>  within Editor::readFile().  So
>>> there
>>> is an instance of Editor when the operator is called.
>>>        
>> But QDataStream&operator>>  is not a member function, so it lacks the
>> implied this->  pointer. It can't access member functions without
>> explicit knowledge of the instance.
>>
>> I think the way you have declared QDataStream&operator>>, you must
>> read it into an object of type Token. Then within readfile(), where
>> you *do* have access to this->  for Editor, you would put the Token
>> into partnerMap.
>>      
> But the data I want to put in partnerMap is not of part of Token, but
> just metadata needed for constructing a valid Token.  So I guess I have
> to make a class TmpToken, which holds this kind of data and will be
> copied to Token within readfile().
>
> I hoped I could circumvent this somehow.
>
>    
If the data is not part of Token, then you should not have Token' stream 
operators store and read it. Simple.

You say you have a map of tokens. Why not let Qt store that map for you 
(if it is a QMap or a QHash)? It could be as simple as

out << partnerMap;

to write it out, and:

in >> partnerMap;

to read it back.

If you look at the docs, you'll find that Qt simply stores the number of 
items in the map, and then all the key/value pairs. You can do the same 
manually if you want.

André




More information about the Qt-interest-old mailing list