const有什么区别? [英] What difference does const make?

查看:98
本文介绍了const有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作了两个班级系和学生,然后成了第三个班级学院,我在其中宣布了一个部门和学生班级的对象。

在大学班级的建设者中:



I have made two classes Department and Student and then made third Class College in which i declared one object each of Department and Student class.
In the constructor of College class:

College(string clg_name, int num_dept, Department &dp,  Student &s)
    {
        m_clgName=clg_name;  
        m_numDept=num_dept;
        dep=dp;
        st=s;
    }





我收到错误:



I am getting error:

error: No matching function for call to 'College::College(const char[5],int ,Department, student)
 no known conversion for argument 3 'Department' from 'Department&'

-------------------------------------------------------------------------
 
 Solution either: College(string clg_name, int num_dept, Department dp,  Student s)
  or    College(string clg_name, int num_dept, const Department &dp, const Student &s)



所以有什么区别呢const构造函数定义吗?





-------------------- -------------------------------------------------- -----



整个代码是这样的:

首先我有一个Student.h文件和Department.h给学生和部门类


so what difference does const make in constructor definition ?


---------------------------------------------------------------------------

Whole code is like this:
First I have a Student.h file and Department.h for student and department class

#ifndef STUDENT_H
#define STUDENT_H

#include <iostream>
#include <cstring>

using namespace std;

class Student
{
    string m_stName;
    int m_stID;
    //
public:
    Student(){}
    Student(string name, int id){StdInfo(name,id);}
    string GetStdName() {return m_stName;}
    int GetStdID(){return m_stID;}
    void StdInfo(string name, int id)
    {
        m_stName=name;
        m_stID=id;
    }
    friend ostream& operator<<(ostream &os, const Student &st);
    friend istream& operator>>(istream &is, Student &st);
};
ostream& operator<<(ostream &os, const Student &st)
{
    os<<"\nStudent's name: "<<st.m_stName<<", ID: "<<st.m_stID<<endl;
    return os;
}
istream& operator>>(istream &is, Student &st)
{
    cout << "\nEnter student's name: ";
    is >> st.m_stName;
    cout << "\nEnter student's ID: ";
    is >> st.m_stID;
    return is;
}
#endif // STUDENT_H 










#ifndef DEPARTMENT_H
#define DEPARTMENT_H

#include <iostream>
#include <cstring>

using namespace std;

class Department
{
    string m_DeptName;
//
public:
    Department(){}
    Department(string name){ setDepName(name);}
    void setDepName(string name){ m_DeptName=name;}
    string getDeptName(){ return m_DeptName;}
    friend ostream& operator<<(ostream &os, const Department &dp);
    friend istream& operator>>(istream &is, Department &dp);
};

ostream& operator<<(ostream &os, const Department &dp)
{
    os<<"Department's name: "<<dp.m_DeptName<<endl;
    return os;
}
istream& operator>>(istream &is, Department &dp)
{
    cout << "\nEnter Department name: ";
    is >> dp.m_DeptName;
    return is;
}
#endif // DEPARTMENT_H 







最后的主要剧本:








finally main script:


#include <iostream>
#include <string>
#include <cstring>
#include "Student.h"
#include "Department.h"

using namespace std;


class College
{
    string m_ClgName;
    int m_numDept;
    Department dep;
    Student st;
    //Department *dep=new Department[3];
    //Student *st=new Student[4];
    College(){}
public:
    College(string clg_name, int num_dept, Department &dp, Student &s)
    {
        m_ClgName=clg_name;
        m_numDept=num_dept;
        dep=dp;
        st=s;
    }
    friend ostream& operator<<(ostream &os, const College &cl);
    friend istream& operator>>(istream &is , College &cl);
    void SetClgDetails()
    {cin >> dep;cin >> st;}
};

ostream& operator<<(ostream &os, const College &cl)
{
    os <<cl.m_ClgName<<" has "<<cl.m_numDept<<" departments\n";
    cout <<cl.dep;
    cout <<cl.st<<endl;
}
istream& operator>>(istream &is , College &cl)
{
    cout << "\nEnter college name :";
    is >> cl.m_ClgName;
    cout << "\nEnter no. of departments: ";
    is >> cl.m_numDept;
}
int main()
{

    College clg("ABCD", 1, Department("Computer_Science"),Student("Rajender",249));
    cout << clg;
    cout << "===============================================";
    cin >>clg;
    clg.SetClgDetails();
    cout <<clg;
    return 0;
} 







我也想知道如何制作Department的对象数组和大学班级的学生类似:








I also want to know how can I make array of objects of Department and Student in College class something like:


Department *dp = new Department[size_d];
Student *st= new Student[size_s]; 





其中size_d和size_s将成为College类的对象,并将根据用户输入来决定用户想要的部门和学生数量以及应该如何构造函数定义应该在这些情况下。



where size_d and size_s will be objects of College class and will take user input to decide how many department and students user wants and how should constructor definition should be in these cases.

推荐答案

看看这里: http://duramecho.com/ComputerInformation/WhyHowCppConst.html [ ^ ]


错误可能不是来自部门学生参数,但是由 clg_name 参数。这些函数需要一个 string 类型的参数,但是你传递了一个 const char * (看看它会很有帮助函数调用)。



const 没有真正的区别但是首选方式:它确保传递 College 构造函数无法更改对象。



因此您应该将构造函数更改为(任一或两者):

The error is probably not sourced by the Department and Student parameters but by the clg_name parameter. The functions expects a parameter of type string but you are passing a const char * (it would be helpful to see the function call).

The const makes no real difference but is the preferred way: It ensures that the passed objects can't be changed by the College constructur.

So you should change the constructor to (either one or both):
College(const string & clg_name, int num_dept, const Department &dp, const Student &s)
{
}
College(const char * clg_name, int num_dept, const Department &dp, const Student &s)
{
}



请注意,第一个版本也通过引用传递名称。实现第二个版本时,您可以使用第二个版本定义第一个版本:


Note that the first version passes also the name by reference. When implementing the second version you can define the first one using the second one:

College(const string & clg_name, int num_dept, const Department &dp, const Student &s)
{
    College(clg_name.c_str(), num_dept, dp, s);
}





[更新]

问题来源于由用于 College Department Student 类的编译器$ c>构造函数。传递引用时,参数必须为const( const Department& dp )。只有在按值传递参数时才允许使用非const参数( Department dp )。请参阅复制分配操作员 [ ^ ]和复制和交换 [ ^ ]。



关于数组,你是正确的。通常的解决方案是提供一个函数来设置成员并为每个数组项调用它。



[UPDATE]
The problem is sourced by the copy assignment operators generated by the compiler for the Department and Student classes which are used in the College constructor. When passing a reference, the parameter must be const (const Department &dp). Non-const parameters are only allowed when passing parameters by value (Department dp). See the Copy assignment operator[^] and Copy-and-swap[^].

Regarding the arrays you are on the right way. The usual solution is to provide a function to set the members and call that for each array item.


您正在将临时对象传递给接受引用的函数。这是不允许的。更改签名以接受const引用会修复它,因为您提供的保修不会触及它。
You are passing a temporary object to a function accepting a reference. That's not allowed. Changing the signature in order to accept a const reference fixes it because you are providing the warranty of not touching it.


这篇关于const有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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