Click here to Skip to main content
15,904,638 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

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

return *this;
}

i tested this code in following statements

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 '-' doing 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 2:25am
v3

I don't see what this has to do with Inheritance, but hey!

When you use
return (int)  CTimeSpan::GetSeconds() ;
Which instance of a CTimeSpan are you trying to get the number of seconds from? If you don't specify an instance, where is the seconds count going to come from?

It's like saying "what is the difference between these two?" and then not letting you see the contents of either of my hands!


"I am just typecasting the value returned by the function GetSeconds() of CTimeSpan class"

The (int) is indeed just a cast - from a LONG to an int

The CTimeSpan::GetSeconds() bit is what I was talking about. You need to construct an instance:
CTimeSpan ts = SecondTime - FirstTime;
return (int) ts.GetSeconds();
 
Share this answer
 
v2
Comments
Niklas L 7-Apr-11 3:21am    
Inheritance would imply the *this instance.
vallikelam 7-Apr-11 3:40am    
hello OrigenalGriff
i didn't add any code in my class(same as CTimeSpan class):inheriting only CTimeSpan class, in my class i want integer values thats why i am typecasting to those functions
OriginalGriff 7-Apr-11 3:54am    
My mistake, sorry about that: I can only claim lack of caffeine!
See the answer Niklas gave re your negative numbers!
Olivier Levrey 7-Apr-11 3:41am    
This is not necessary in C++. This syntax is legal and implicitly use this pointer. As an alternative you may use __super (but I think it is Microsoft specific).
If you get negative values, you have probably created a negative time span, maybe by using CTime::operator-() with the greatest time on the right hand side.
CTime time1(2011, 4, 7, 9, 0, 0);
CTime time2(2011, 4, 7, 9, 0, 10);

CTimeSpan span1 = time2 - time1;
ASSERT(span1.GetSeconds() > 0);

CTimeSpan span2 = time1 - time2;
ASSERT(span2.GetSeconds() < 0);
 
Share this answer
 
Comments
Olivier Levrey 7-Apr-11 3:41am    
This seems to be the more logical answer. My 5.
From CTimeSpan documentation[^]:

Returns the number of seconds in the current minute. The range is –59 through 59.


So why are you surprised (i.e. please explain why the instance of CVoipTimeSpan shouldn't return negative values)?

 
Share this answer
 
v3
Comments
vallikelam 7-Apr-11 3:54am    
I know the range I am asking positive values why not we got that?
CPallini 7-Apr-11 4:11am    
Hence more code is needed here, you know.

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