CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2002
    Posts
    309

    How to create conversion of independent types?

    Code like this

    double n =0;
    CTime(n);

    generates warning
    "warning C4244: 'initializing' : conversion from '__time64_t' to 'double', possible loss of data"

    probably need to write a conversion to make compiler
    notified that conversion proceed through redefined way -
    loss of data is in aware of program.

    How to write this, with probably no inherit from
    that types - due many code already written,
    and will be looking no well readable with so many conversions.

    Using SmartPointers only?

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Why don't you create CTime in the terms it can be created? Look
    at this link:
    http://msdn.microsoft.com/library/de...3a3a.ctime.asp.

    Note that you don't have the option to create a CTime object from
    a double. So, convert your double to a type that you can create
    a CTime from and then create the CTime object.

    --Paul

  3. #3
    Join Date
    Aug 2002
    Posts
    309
    This is due to problems:

    first - this is common problem and I want have to know a solution.

    second - code is written for a duoble, it was suitable for to see and to use format. Code desired to be not fully rewritten.

    Milliseconds was not found in most of times,
    except system time - but it was huje, no opearations (+, -) defined for this.
    Direct operations as on seconds and parts of seconds is not presented. Does this correctl?

    Doew problem can be solved not changing problem self to
    much smaller subset.

    Code:
    typedef struct _SYSTEMTIME {  WORD wYear;  WORD wMonth;  WORD wDayOfWeek;  WORD wDay;  WORD wHour;  WORD wMinute;  WORD wSecond;  WORD wMilliseconds;
    } SYSTEMTIME, *PSYSTEMTIME;
    Last edited by vgrigor3; June 9th, 2003 at 09:11 AM.

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    I don't really understand what you're saying. I think you mean
    that you have some code that you guys use in-house which uses
    double for your time types. You also want to use CTime so you
    are trying to convert double to a CTime.

    I think you're going to want to use CTime's interface to set it
    based on your double. You could probably create a wrapper
    function to do something like this:
    Code:
    CTime createTimeFromDouble(double ourTime)
    {
       CTime time;
       time.setMonth(/*...*/);
       // ...
       return time;
    }

  5. #5
    Join Date
    Aug 2002
    Posts
    309
    No,
    problem in no writing just working code-
    (rewrite all method, even al works)
    but "right" code -good readable,
    once- changed for all-workable,

    so there is much
    code like I shown,
    and need in one place, not changing all this occurencies,
    eliminate warning - make the conversions approved by language.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by vgrigor3
    No,
    problem in no writing just working code-
    (rewrite all method, even al works)
    but "right" code -good readable,
    once- changed for all-workable,

    so there is much
    code like I shown,
    and need in one place, not changing all this occurencies,
    eliminate warning - make the conversions approved by language.
    You cannot do this. You either have to

    a) derive a class from CTime and create a constructor that takes
    a double,

    OR

    b) Create a class wrapper for CTime (derive a class that contains
    a CTime object)

    OR

    c) Call a function that returns a CTime based on your double argument.

    OR

    d) Create a class that takes a double (call it DateDouble) and has
    a conversion function that returns some argument that is
    appropriate for CTime. Then instead of "double d = 1.0", you do

    DataDouble d = 1.0;
    CTime( d );

    where DataDouble is a class that takes a double and has a
    conversion operator that converts the 1.0 to the appropriate type.

    Bottom line is that there is no magic bullet that will work the way
    you want it to work. When you want to make a class take an
    argument of your own choosing, you have no choice but to do
    one of the four items above. The constructor for the original
    class cannot be changed (unless you have the source code).

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Aug 2002
    Posts
    309
    most good variant can picked

    DataDouble d = 1.0;
    CTime( d );

    Probably I'll use this due to not double is not
    standard time type and is used occasionally due to suitability,
    in only intermediate places - for saving place and precision.

    But else variants is desired -due to probably not all variants you
    found in small time period and C++ is reach.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured