继承CTimeSpan类 [英] Inheriting CTimeSpan class

查看:69
本文介绍了继承CTimeSpan类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在基类CVoipTimeSpan中继承CTimeSpan类.我正在打印所有成员函数,如GetDays(),GetMinuts()..它给出负值,我怎么能得到正值.我在项目中使用CVoipTimeSpan代替CTimeSpan.在此显示了一些错误,例如
错误C2679:二进制="=":未定义运算符,该运算符采用类型为"CTimeSpan"类的右侧操作数(或没有可接受的转换)
是否可以继承运算符?
请给我答复.



您好,在将CTimeSpan类继承到我的类CVoipTimeSpan后,操作符="="没有被继承,然后我将代码写入继承="="的操作符为

Hello
I am inheriting CTimeSpan class in my base class CVoipTimeSpan. I am printing all the member functions like GetDays(), GetMinuts().. it gives negative values, how can i get positive values. I am using CVoipTimeSpan in place of CTimeSpan in my project. In this it shows some errors like
error C2679: binary ''='' : no operator defined which takes a right-hand operand of type ''class CTimeSpan'' (or there is no acceptable conversion)
can we inherit the operators or not?
please give me the reply.



Hello 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();

推荐答案

运算符的问题很简单:您当然继承了它,但它在基础上定义了运算符类,但您尝试使用派生类进行操作,并且可能未定义隐式转换运算符.因此,您需要定义需要包括派生类型的运算符:赋值以及您可能需要的其他任何内容.您可能还需要定义转换并复制构造函数.

至于您的消极价值观,请参阅我对问题的评论.如果您无法弄清楚,请添加一些代码,但请保持最低限度.


(在获得更多信息之后.)

您正在尝试定义将CVoipTimeSpan分配给CVoipTimeSpan的赋值运算符,但是您的错误是关于将CTimeSpan分配给CVoipTimeSpan的.您不是要定义此运算符.使用参数(const CTimeSpan& entry);它将代表作业的右手部分.

至于负时间跨度,我在这里看不到问题:时间T1比时间T2早,T2-T1应该是正时间跨度,而T1-T2应该是负时间跨度,是吗?是不是很自然?

—SA
The problem with operator is simple: you inherit it of course, but it defines the operators on the base class, but you try to operate with your derived class, and you probably did not define implicit conversion operator. So, you need to define the operator(s) you need to include your derived type: assignment and whatever else you may need. You also may need to define conversions and copy constructors.

As to your negative values, see my comment to the Question. If you cannot figure it out, add some code, but please keep it to essential minimum.


(After additional information.)

You''re trying to define assignment operator for assigning CVoipTimeSpan to CVoipTimeSpan, but your error is about assigning CTimeSpan to CVoipTimeSpan. You''re not trying to define this operator. Use the argument (const CTimeSpan& entry); it will represent right-hand part of assignment.

As to a negative time span I don''t see a problem here: It time T1 is sooner than time T2, T2-T1 should be positive time span, and T1-T2 should be negative time span, isn''t that natural?

—SA


也许自从我正在使用的VS 2003以来,这已经发生了变化,但是我看不到这里定义的
Maybe this has changed since VS 2003 which I''m using at the moment, but I see no
CTimeSpan::operator=()

,所以您不能不可能从中继承.编译器将自动为任何未定义自身的类创建一个operator=,但它不是虚拟的,因此不能被继承和覆盖,只能被覆盖!

此外,即使存在方法

defined here, so you couldn''t possibly inherit from it. The compiler will automatically create an operator= for any class that doesn''t define one itself, but it will not be virtual, and thus can not be inherited and overridden, only overwritten!

Moreover, even if there were a method

virtual CTimeSpan::operator=(const CTimeSpan&)

,然后要覆盖它,您也必须对参数使用相同的类型,在本例中为const CTimeSpan&,而不是const CVoipTimeSpan&.但是,这将是不好的设计,因为它将允许分配类CTimeSpan或从其派生的其他类的实例,这些实例可能与CVoipTimeSpan不兼容.即使现在不存在兼容性问题,当以后有人决定扩展您的类CVoipTimeSpan时,这也会造成这种不兼容性,从而不仅使operator=()的实现无效,而且可能会使许多地方变得无效.它已被调用!

我已经在您的另一个话题中指出了如何正确实现

then to override it you must use the same types for the parameter, in this case const CTimeSpan&, not const CVoipTimeSpan&. This would be bad design however, since it would allow assignments of instances of class CTimeSpan or other classes derived from it, which are potentially not compatible to CVoipTimeSpan. Even if there would be no compatibility issues now, when someone later decides to expand your class CVoipTimeSpan, this might create such an incompatibility and thus invalidate not only your implementation of operator=(), but also, possibly, many of the places where it has been invoked!

I''ve already pointed out in another thread of yours how to properly implement

CVoipTimeSpan::operator=(const CVoipTimeSpan&)

,据我所知,所有其他问题都已得到解答.

在旁注中,我已经看到您的问题现在至少发布了三遍! 不要这样做!这样做只会使可能能够帮助您的人不知道已经分发的其他信息和建议.基本上,您会在使其他人的工作量倍增的同时,又对基于不同假设的不同线程做出混乱的响应.相反,只需编辑您原来的问题!

现在有点晚了.但是,请至少帮我们一个忙:
-如果问题已解决,则将所有线程标记为已回答,或者
-如果仍然有问题,请将您除一个线程外的所有线程标记为已回答
-在您的每个线程中编辑原始问题,以使其他线程知道重复的线程,并可能在该线程中插入您想要其他答案的链接.

, and as far as I can see all other questions have been answered.

On a sidenote, I''ve seen your question posted at least three times now! Do not do that! It will just keep people who might be able to help you ignorant of additional information and advice that''s already been handed out. Basically you multiply the workload on other people while getting confusing responses to different threads that are based on different assumptions. Instead, just edit your original question!

It''s a bit late now. However, do us the favor to at the very least:
- mark all of your threads as answered if your problem is solved, or
- mark all but one of your threads as answered if there are still questions
- edit the original question in each of your threads to make others aware of the duplicate threads, and maybe insert a link to that one thread that you would like any additional answers in.


这篇关于继承CTimeSpan类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆