 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Anoop Aryal Guest
|
Posted: Tue Mar 29, 2005 9:30 am Post subject: subclassing standard C structs. |
|
|
hi all,
i'm working on a project where it used time_t initially (from <ctime>). i
needed more precision so the natural choice was to use timeval structs.
then i faced the prospect of changing all code for doing subtraction etc to
deal with the timeval structs instead of the straight subtraction of
time_t.
then i figured, i could create a subclass of timeval like so:
class Timeval : public timeval {
public:
Timeval operator-(const Timeval& other);
/* etc... */
};
that way, i can use the standard C lib (eg. gettimeofday) all the while
using C++ syntax within my own project as it was written for time_t data
types (by providing all the operators in the Timeval class that were used
initially on the time_t data types).
are there any drawbacks of C++-a-fying the standard C structs? and if one
were to get some grandiose ideas, could this be used as a way to provide a
C++ wrapper to standard Unix C structs? (by subclassing the struct into a
class).
feedback much appreciated.
anoop aryal.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
wij@seed.net.tw Guest
|
Posted: Wed Mar 30, 2005 11:31 pm Post subject: Re: subclassing standard C structs. |
|
|
Anoop Aryal wrote:
| Quote: | ....
that way, i can use the standard C lib (eg. gettimeofday) all
the while using C++ syntax within my own project as it was
written for time_t data types (by providing all the operators
in the Timeval class that were used initially on the time_t
data types).
|
There is a developing library might work for your project.
http://sourceforge.net/projects/libwx/
there, timespec is used instead of timeval, now() is used for
gettimeofday(..)
| Quote: | are there any drawbacks of C++-a-fying the standard C structs?
and if one were to get some grandiose ideas, could this be used
as a way to provide a C++ wrapper to standard Unix C structs?
(by subclassing the struct into a class).
|
In basic, no particuliar drawbacks except less 'grandiose'.
Drawbacks are probably general for all 'C++-a-fying'.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
simont Guest
|
Posted: Wed Mar 30, 2005 11:32 pm Post subject: Re: subclassing standard C structs. |
|
|
| Quote: | hi all, i'm working on a project where it used time_t initially (from
ctime>). i needed more precision so the natural choice was to use
timeval structs. then i faced the prospect of changing all code for
doing subtraction etc to deal with the timeval structs instead of the
straight subtraction of time_t.
then i figured, i could create a subclass of timeval like so
|
<snip>
Why not just declare free non-member operators?
Eg.
timeval operator - (const timeval& a, const timeval& b);
timeval operator + (const timeval& a, const timeval& b);
etc.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Mar 30, 2005 11:35 pm Post subject: Re: subclassing standard C structs. |
|
|
Anoop Aryal wrote:
| Quote: | hi all,
i'm working on a project where it used time_t initially (from <ctime>). i
needed more precision so the natural choice was to use timeval structs.
then i faced the prospect of changing all code for doing subtraction etc to
deal with the timeval structs instead of the straight subtraction of
time_t.
|
As long as you're not adding anything that would require change in the
construction/destruction/copying semantics, the changes are largely
transparent. Just adding a few operator overloads (for operators
that are otherwise not implemented) will work fine.
[ 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
|
|