C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

subclassing standard C structs.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Anoop Aryal
Guest





PostPosted: Tue Mar 29, 2005 9:30 am    Post subject: subclassing standard C structs. Reply with 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:

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





PostPosted: Wed Mar 30, 2005 11:31 pm    Post subject: Re: subclassing standard C structs. Reply with quote



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





PostPosted: Wed Mar 30, 2005 11:32 pm    Post subject: Re: subclassing standard C structs. Reply with quote



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





PostPosted: Wed Mar 30, 2005 11:35 pm    Post subject: Re: subclassing standard C structs. Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.