Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > newbie: simple question

Reply
Thread Tools

newbie: simple question

 
 
Michal
Guest
Posts: n/a
 
      04-08-2009
hallo people.

how to write C++ style conversion in order for it to compile?

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

int main(int argc, char *argv[])
{
PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
duration.first = 3;
cout << duration.first << endl;
return 0 ;
}


best regards,
Michal
 
Reply With Quote
 
 
 
 
Vladyslav Lazarenko
Guest
Posts: n/a
 
      04-08-2009
On Apr 8, 10:21*am, Michal <(E-Mail Removed)> wrote:
> hallo people.
>
> how to write C++ style conversion in order for it to compile?
>
> #include <sys/time.h>
> #include <algorithm>
> #include <iostream>
>
> using namespace std;
>
> typedef pair<unsigned long long, struct timeval> PerfDebData;
>
> int main(int argc, char *argv[])
> {
> * PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
> * duration.first = 3;
> * cout << duration.first << endl;
> * * return 0 ;
>
> }
>
> best regards,
> Michal


timeval is a C structure and does not have constructors. So you have
to initialize values of that structure yourself. But you cannot do it
like you tried. This is how you can do:

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

int main(int argc, char *argv[])
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
PerfDebData duration(0, tv);
duration.first = 3;
cout << duration.first << endl;
return 0 ;
}

For convenience, you may create a free function initializing timeval
structure:

#include <sys/time.h>
#include <algorithm>
#include <iostream>

using namespace std;

typedef pair<unsigned long long, struct timeval> PerfDebData;

template <typename TimeT, typename SecT>
static inline timeval make_timeval(TimeT sec, SecT usec) {
timeval res;
res.tv_sec = sec;
res.tv_usec = usec;
return res;
}

int main(int argc, char *argv[])
{
PerfDebData duration(0, make_timeval(1, 2));
duration.first = 3;
cout << duration.first << " -> (" << duration.second.tv_sec << ", "
<< duration.second.tv_usec << ")." << endl;
return 0 ;
}
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      04-08-2009
On Apr 8, 10:34*am, Pete Becker <(E-Mail Removed)> wrote:
> On 2009-04-08 10:21:34 -0400, Michal <(E-Mail Removed)> said:
>
>
>
> > hallo people.

>
> > how to write C++ style conversion in order for it to compile?

>
> > #include <sys/time.h>
> > #include <algorithm>
> > #include <iostream>

>
> > using namespace std;

>
> > typedef pair<unsigned long long, struct timeval> PerfDebData;

>
> > int main(int argc, char *argv[])
> > {
> > * PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
> > * duration.first = 3;
> > * cout << duration.first << endl;
> > * * return 0 ;
> > }

>
> timeval tv = { 0, 0 };
> PerfDebData duration(0, tv);
>


Pete, wouldn't this work as well:

PerfDebData duration(0, timeval());

Since the () version of the constructor would zero-initialize all
members of timeval?

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-09-2009
On Apr 8, 4:21 pm, Michal <(E-Mail Removed)> wrote:

> how to write C++ style conversion in order for it to compile?


> #include <sys/time.h>
> #include <algorithm>
> #include <iostream>


> using namespace std;


> typedef pair<unsigned long long, struct timeval> PerfDebData;


Note that the "struct" isn't necessary.

> int main(int argc, char *argv[])
> {
> PerfDebData duration(0, static_cast<struct timeval>({0, 0}));
> duration.first = 3;
> cout << duration.first << endl;
> return 0 ;
> }


If you want C++ style "conversions", you'll have to provide a
class with constructors. It's generally not worth it (just
provide a free function, as someone else suggested, and only
that if you need it more than once), you can derive from
timeval, and use the derived class everywhere in your C++. (Of
course, any C function which returns a timeval or a timeval*
will still continue to do so. So you're derived class should
have a constructor which takes one or both of these.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Michal
Guest
Posts: n/a
 
      04-09-2009
Pete Becker <(E-Mail Removed)> writes:

> Since the () version of the constructor would zero-initialize all
> members of timeval?


Sure, for all-zero initialization. Using an initializer lets you
provide other values as well.


Is it some part of standard, I mean, can You also do:
int a() to have a initialized with 0?

 
Reply With Quote
 
Michal
Guest
Posts: n/a
 
      04-09-2009
> Not exactly, for reasons of poor syntax. Instead, use:
>
> int a = int( );



so just to summarize:

int() and timeval() both create 0 initialized variables of their type,
while:

timeval a() and int b() do not guarantee it.

is it correct?
 
Reply With Quote
 
Michal
Guest
Posts: n/a
 
      04-10-2009
Jeff Schwab <(E-Mail Removed)> writes:

>
> http://www.parashift.com/c++-faq-lit....html#faq-10.2
>
> In the current draft standard, go to clause 8.5 [dcl.init], and see
> the note at paragraph 10.
>
> http://www.open-std.org/jtc1/sc22/wg...2008/n2798.pdf
>
> IIU(clause 8.5.4)C, objects in C++0x may be default- or
> value-initialized using list initialization syntax:
>
> timeval a{};
> int b{};



Thank You all for the response
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Plz, a simple answer to a simple question about IP addresses MeekiMoo Computer Support 0 07-28-2009 08:10 AM
Simple region code question... simple answer?? joseph.greer@gmail.com DVD Video 7 01-26-2007 09:07 PM
Simple Question - Simple Answer? Daniel Frey XML 4 01-12-2005 04:25 PM
Re: Simple Simple question!!! Kevin Spencer ASP .Net 0 06-25-2004 05:25 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments