+ -
当前位置:首页 → 问答吧 → 请教高手一个基础问题。很简单,基础知识

请教高手一个基础问题。很简单,基础知识

时间:2011-12-15

来源:互联网

问个基础问题啊:
这个是头文件
C/C++ code
#ifndef kaoni_h_
#define kaoni_h_
#include <iostream>
class Time
{
private:
        int hours;
        int minutes;
public:
    Time();
    Time(int h, int n);
    void addmin (int m);
    void addhr (int h);
    friend Time operator + (const Time & t1,const Time & t2);
    friend Time operator - (const Time & t1,const Time & t2 );
    friend Time operator * (double a,Time & t1);
    friend Time operator * (Time & t1, double a);
    friend std::ostream & operator<< (std::ostream & os , const Time & t1);
};

#endif



这个是具体实线:
C/C++ code
// mytime0.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "mytime.h"
#include <iostream>

Time::Time()
{
    minutes = hours = 0;
}

Time::Time(int h, int m)
{
    hours = h;
    minutes = m;
}

void Time::addmin ( int m)
{
    minutes += m;
    hours += minutes/60;
    minutes +=minutes%60;
}

void Time::addhr(int h)
{
    hours += h;
}

Time operator + (const Time & t1,const Time & t2)
{
    Time sum;
    sum.minutes = t1.minutes +t2.minutes;
    sum.hours = sum.minutes/60 + t1.hours +t2.hours;
    sum.minutes = sum.minutes % 60;
    return sum;
}

Time operator - (const Time & t1,const Time & t2 )
{
    Time diff;
    int total1, total2;
    total1 = t1.minutes +60 * t1.hours;
    total2 = t2.minutes + 60 * t2.hours;
    diff.minutes = total1 - total2;
    diff.hours = diff.minutes/60;
    diff.minutes = diff.minutes % 60;
    return diff;
}

Time operator * (double a,Time & t1)
{
    Time result;
    long totalminutes = t1.hours * a *60 +t1.minutes * a;
    result.minutes = totalminutes;
    result.hours = result.minutes/60;
    result.minutes = result.minutes % 60;
    return result;
}

Time operator * (Time & t1, double a)
{
    Time result;
    long totalminutes = t1.hours * a *60 +t1.minutes * a;
    result.minutes = totalminutes;
    result.hours = result.minutes/60;
    result.minutes = result.minutes % 60;
    return result;

}
std::ostream & operator<< (std::ostream & os , const Time & t1)
{
    os << t1.hours << "hours, " << t1.minutes << " minutes\n";
    return os;
}


我问的问题是我我为什么不能把对<<的操作符重载的声明和定义的引用全部丢了,为什么不能按值传递啊。很清楚的问题
希望高手详细点,越详细,分数占比越高

作者: zjxzjx54   发布时间: 2011-12-15

因为operator <<要有能够修改std::ostream的能力,如果不是引用的话,就 不能修改传入的os对象。也就不能输出了。返回值是引用是为了能够支持这样的语法:

os<<time<<"somthing";//连续的《《

作者: mingliang1212   发布时间: 2011-12-15

ostream类型的对象是不支持复制的,只能传引用

作者: qscool1987   发布时间: 2011-12-16

你说的是返回值处的引用还是参数Time类的引用?

如果是返回值,则一定要使用引用,原因如下:(百度的...)
  流操作符<<和>>,这两个操作符常常希望被连续使用,例如:cout << "hello" << endl; 因此这两个操作符的返回值应该是一个仍然支持这两个操作符的流引用。可选的其它方案包括:返回一个流对象和返回一个流对象指针。但是对于返回一个流对象,程序必须重新(拷贝)构造一个新的流对象,也就是说,连续的两个<<操作符实际上是针对不同对象的!这无法让人接受。对于返回一个流指针则不能连续使用<<操作符。因此,返回一个流对象引用是惟一选择。这个唯一选择很关键,它说明了引用的重要性以及无可替代性,也许这就是C++语言中引入引用这个概念的原因吧。 赋值操作符=。这个操作符象流操作符一样,是可以连续使用的,例如:x = j = 10;或者(x=10)=100;赋值操作符的返回值必须是一个左值,以便可以被继续赋值。因此引用成了这个操作符的惟一返回值选择。

如果是参数的问题,我记得是没有要求一定要使用引用的,但是还是建议使用引用,如果不想改变值加个const就行了,这样效率会高点

作者: Johnkey_Chen   发布时间: 2011-12-16

使用explicit修饰了构造函数的类对象就不能做单值赋值了,除非实现有拷贝构造函数,或者重载了=操作符~

作者: mscf   发布时间: 2011-12-16