不推荐使用从字符串常量转换为'char *'c ++

时间:2015-11-30 15:28:47

标签: c++

class employee {
        public :
            employee (char *inName, char *inPosition, double inSalary =0.0);
            ~employee();
            char getName()const;
            char getPosition()const;
            double getSalary()const;
            virtual void display();
        private :
            char name[40];
            char position[40];
            double salary;
        protected:
            void setName(char inName);
            void setPosition(char inPosition);
            void setSalary(double inSalary);
};
employee::employee(char *inName, char *inPosition, double inSalary){
    setName(*inName);
    setPosition(*inPosition);
    setSalary(inSalary);
}
//destructor
//setter
//getter
void employee::display(){
    cout<<"Employee Name Is     :"<<getName()<<endl;
    cout<<"Employee Position Is :"<<getPosition()<<endl;
    cout<<"Employee Salary  :"<<getSalary()<<endl;
}
int main{
    char *x ="bello";
    employee e1(x, "123",50);
    e1.display();
}

我怎么可能像我的员工e1(x,“123”,50)那样解决这个问题, 我收到一条消息:已弃用从字符串常量转换为'char *。什么是可能的解决方案?

1 个答案:

答案 0 :(得分:2)

嗯,这个;

char *x ="bello";

应该是:

const char *x ="bello";

原因是c ++字符串文字的类型为const char[]

同样适用于(声明和定义):

employee (char *inName,     char *inPosition, double inSalary =0.0);
          ^~~~here consts ~^~