 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jaap Suter Guest
|
Posted: Sat Jun 28, 2003 11:16 pm Post subject: Prefix and postfix operation on operator -> |
|
|
Hi,
I have a smartpointer type, where I want to perform an operation before and
after each dereference. I tried the following:
template < class T >
class sptr
{
public:
proxy operator -> () { return proxy( mPtr ); }
private:
class proxy
{
proxy( T* t ) : mPtr( t ) { prefix_op(); }
~proxy() { postfix_op(); }
T* operator -> { return mPtr; }
private:
T* mPtr;
};
T* mPtr
};
This works on MSVC but fails on GCC 2.9.x, because of different lifetimes of
objects. On MSVC the following code:
sptr< bar > p( ... );
p->foo();
does the following:
prefix_op();
bar::foo();
postfix_op();
whereas on GCC, the following happens:
prefix_op();
postfix_op();
bar::foo();
which is bad because the postfix shuts something down that is needed in
bar::foo();. I was wondering whether the standard says anything about the
lifetime of the proxy object in the code above. Futhermore, is there a way
to achieve what I am trying to achieve?
Regards,
Jaap Suter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Marco Manfredini Guest
|
Posted: Sun Jun 29, 2003 10:17 pm Post subject: Re: Prefix and postfix operation on operator -> |
|
|
Jaap Suter wrote:
| Quote: | This works on MSVC but fails on GCC 2.9.x, because of different
lifetimes of objects. On MSVC the following code:
sptr< bar > p( ... );
p->foo();
does the following:
prefix_op();
bar::foo();
postfix_op();
|
Which is the correct behaviour.
| Quote: |
whereas on GCC, the following happens:
prefix_op();
postfix_op();
bar::foo();
|
Which is bad, since temporaries shall exist during the lifetime of the
operation they are involved into. This is probably a problem with the
gcc you are using. gcc 3.2.2 here gets it right and I remember that
earlier 2.95.x versions did that right too, because I've used the same
technique to implement handle-to-pointer locking during calls once.
Regards
Marco
--
The text above is a result of a bug in my newsreader and I take no
responsibility for this text appearing in my post.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|