T
TuxC0d3
Hi!
I'm diving into the some more ++ specific aspects of c++ (and finally
accepting that c++ is more than "a plus added to c" , so that means
using namespaces, templates, std::strings, lists, vectors, operator
overloading and what not.. And i was wondering if there is a way to
override the global dereference operator, so to be able to check the
address that one tries to dereference. This gives the ability to throw
an exception when one would try to dereference NULL for instance.
I have tried several different ways to do it, but gcc doesn't like what
i'm asking of him...
This attempt is the most reasonable to me:
43 template<class T> T& operator * (T* ptr)
44 {
45 if (!ptr)
46 throw EAccessViolation();
47
48 return (*ptr);
49 }
(sorry about the line numbers.. Just the vim settings on my console)
Even though this seems right to me, g++ gives me the following
complaint when i compile that like thus:
[tim@server tests]$ g++ -Wall -o test operatornew.cpp > c.log 2>&1
[tim@server tests]$ cat c.log
operatornew.cpp:44: error: `T& operator*(T*)' must have an argument of
class or
enumerated type
[tim@server tests]$
Could it be that the compiler gets confused and/or thinks that i want
to overide the multiply operator? Or am i just beeing totaly clueless
here?
I have searched for this for what feels like all around the net, but i
can't seem to find anything like it. Everything that comes up about
dereference operator overloading is the operator->() for inside a
class.. I have also tried putting this global, which naturaly didn't
work becouse -> is obviously only used for members..
Can anyone give me a lead on this and/or provide some example code?
Tnx!
Tim.
I'm diving into the some more ++ specific aspects of c++ (and finally
accepting that c++ is more than "a plus added to c" , so that means
using namespaces, templates, std::strings, lists, vectors, operator
overloading and what not.. And i was wondering if there is a way to
override the global dereference operator, so to be able to check the
address that one tries to dereference. This gives the ability to throw
an exception when one would try to dereference NULL for instance.
I have tried several different ways to do it, but gcc doesn't like what
i'm asking of him...
This attempt is the most reasonable to me:
43 template<class T> T& operator * (T* ptr)
44 {
45 if (!ptr)
46 throw EAccessViolation();
47
48 return (*ptr);
49 }
(sorry about the line numbers.. Just the vim settings on my console)
Even though this seems right to me, g++ gives me the following
complaint when i compile that like thus:
[tim@server tests]$ g++ -Wall -o test operatornew.cpp > c.log 2>&1
[tim@server tests]$ cat c.log
operatornew.cpp:44: error: `T& operator*(T*)' must have an argument of
class or
enumerated type
[tim@server tests]$
Could it be that the compiler gets confused and/or thinks that i want
to overide the multiply operator? Or am i just beeing totaly clueless
here?
I have searched for this for what feels like all around the net, but i
can't seem to find anything like it. Everything that comes up about
dereference operator overloading is the operator->() for inside a
class.. I have also tried putting this global, which naturaly didn't
work becouse -> is obviously only used for members..
Can anyone give me a lead on this and/or provide some example code?
Tnx!
Tim.