newbie: simple question

Discussion in 'C++' started by Michal, Apr 8, 2009.

  1. Michal

    Michal Guest

    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
     
    Michal, Apr 8, 2009
    #1
    1. Advertising

  2. On Apr 8, 10:21 am, Michal <> 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 ;
    }
     
    Vladyslav Lazarenko, Apr 8, 2009
    #2
    1. Advertising

  3. Michal

    red floyd Guest

    On Apr 8, 10:34 am, Pete Becker <> wrote:
    > On 2009-04-08 10:21:34 -0400, Michal <> 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?
     
    red floyd, Apr 8, 2009
    #3
  4. Michal

    James Kanze Guest

    On Apr 8, 4:21 pm, Michal <> 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:
    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
     
    James Kanze, Apr 9, 2009
    #4
  5. Michal

    Michal Guest

    Pete Becker <> 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?
     
    Michal, Apr 9, 2009
    #5
  6. Michal

    Michal Guest

    > 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?
     
    Michal, Apr 9, 2009
    #6
  7. Michal

    Michal Guest

    Michal, Apr 10, 2009
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    560
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    666
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    844
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    377
    Roland Hall
    Jan 26, 2004
  5. Jerry C.
    Replies:
    8
    Views:
    248
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page