Click here to Skip to main content
15,921,276 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all
After inheriting CTimeSpan class to my class CVoipTimeSpan the operater '=' is not inherited then i write the code to inheriting '=' operator as

C++
const CVoipTimeSpan& CVoipTimeSpan::operator=(const CVoipTimeSpan& entry)
{
    if (entry != *this)
    {
        (CTimeSpan)*this = (CTimeSpan) entry;
    }

    return *this;
}


i tested this code in following statements

C++
CTime curr_time;
CVoipTimeSpan time_span;
int time_diff_in_secs; 
curr_time = CTime::GetCurrentTime();
time_span = curr_time - database_cache_lookup_time;
time_diff_in_secs = time_span.GetTotalSeconds();


the operator '-' done correct and in time_deff_in_secs i got '0' why the operator = gives zero any modifications in my code please reply me .
Posted
Updated 5-May-11 3:13am
v2

CTimeSpan::GetTotalSeconds() returns a LONG LONG value, but you are storing it to an int, this could be the problem. Try running your code through the debugger to see what value is being generated.
 
Share this answer
 
Comments
vallikelam 5-May-11 9:28am    
in my class CVoipTimeSpan i inherited all the members of CTimeSpan class and i typecasting all the functions to int when returning the functoions
Stefan_Lang 5-May-11 11:58am    
Casting is almost always a very very bad idea. If you don't cast, errors like the one Richard pointed out will at least cause the compiler to throw a warning. By inserting a cast, all you do is tell the compiler that it shouldn't care - but you're still trying to measure a pool of water by pouring its contents into a small cup.
Richard MacCutchan 5-May-11 12:13pm    
Well pointed out; I do find that far too many 'programmers' (and I use the word advisedly) think that casting actually offers some form of conversion.
fjdiewornncalwe 5-May-11 9:29am    
Great catch. Like most devs I know, I got caught looking for subtleties instead of the "Is it plugged in" approach. +5
Sergey Alexandrovich Kryukov 5-May-11 18:45pm    
Good catch, my 5. I also answered about operator, please see.
--SA
You're trying to define operator for assigning CVoipTimeSpan to CVoipTimeSpan.
You need an operator to assign CVoipTimeSpan to CTimeSpan.

—SA
 
Share this answer
 
Comments
Niklas L 6-May-11 2:46am    
This is a very likely cause for trouble.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900