【问题标题】:Passing const to function with non-const parameter将 const 传递给具有非 const 参数的函数
【发布时间】:2017-03-30 09:48:27
【问题描述】:

我正在关注加速 C++ 书籍,但我对他们提供的源代码感到困惑。我的困惑涉及函数double grade(double midterm, double final, const vector<double>& hw)double median(vector<double> vec)。 它在评论中说 grade 函数不会复制它的参数,因为 median 函数会复制它的参数。为什么呢? 据我了解,您不能将 const 传递给非常量,因为这需要写访问。

代码如下:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::domain_error;
using std::endl;
using std::istream;
using std::ostream;
using std::setprecision;
using std::sort;
using std::streamsize;
using std::string;
using std::vector;

// compute the median of a `vector<double>'
// note that calling this function copies the entire argument `vector'
double median(vector<double> vec)
{
#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif

    vec_sz size = vec.size();
    if (size == 0)
        throw domain_error("median of an empty vector");

    sort(vec.begin(), vec.end());

    vec_sz mid = size/2;

    return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework)
{
    return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

// compute a student's overall grade from midterm and final exam grades
// and vector of homework grades.
// this function does not copy its argument, because `median' does so for us.
double grade(double midterm, double final, const vector<double>& hw)
{
    if (hw.size() == 0)
        throw domain_error("student has done no homework");
    return grade(midterm, final, median(hw));
}

// read homework grades from an input stream into a `vector<double>'
istream& read_hw(istream& in, vector<double>& hw)
{
    if (in) {
        // get rid of previous contents
        hw.clear();

        // read homework grades
        double x;
        while (in >> x)
            hw.push_back(x);

        // clear the stream so that input will work for the next student
        in.clear();
    }
    return in;
}


int main()
{
    // ask for and read the student's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;

    // ask for and read the midterm and final grades
    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;

    // ask for the homework grades
    cout << "Enter all your homework grades, "
            "followed by end-of-file: ";

    vector<double> homework;

    // read the homework grades
    read_hw(cin, homework);

    // compute and generate the final grade, if possible
    try {
        double final_grade = grade(midterm, final, homework);
        streamsize prec = cout.precision();
        cout << "Your final grade is " << setprecision(3)
             << final_grade << setprecision(prec) << endl;
    } catch (domain_error) {
        cout << endl << "You must enter your grades.  "
            "Please try again." << endl;
        return 1;
    }

    return 0;
}

【问题讨论】:

标签: c++ reference constants


【解决方案1】:

看看这一行:

return grade(midterm, final, median(hw));

hw 这里是一个向量的常量引用,它被传递给median。虽然您不能通过 const 引用修改对象,但您仍然可以通过 const 引用复制它。因此,由于median 需要更改vector(通过对其进行排序),它会为自己创建一个副本(该副本将在后台隐式创建)并对其进行排序。

【讨论】:

  • 那是我丢失的那个位,所以允许中值复制 const。
【解决方案2】:

为什么会这样?据我了解,您不能将 const 传递给 非常量,因为需要写访问权限。

是也不是,您不能写入 const 变量是正确的,但这不是这里发生的事情。

return grade(midterm, final, median(hw));

这里对median 的调用为我们完成了工作。 median 期望 std::vector 并且编译器在引擎盖下传递它。它复制const &amp; 引用的向量并将该副本传递给median。请记住,编译器只需要读取权限来复制,而不是写入,这正是hw 为我们提供的。

【讨论】:

    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多