收到警告"从字符串常量到 char* 的转换已弃用。为什么我会收到警告?
Getting warning "deprecated conversion from string constant to char*. Why am I getting the warnings?
我是C++的初学者。我正在研究继承问题。我已经写了一个代码并编译了它,它似乎运行得很好,我得到了预期的输出。但当我编译它时,我得到了13个类似的警告。我不确定是什么问题?如何覆盖这些警告?这是我的代码:
#include <iostream>
#include <string.h>
using namespace std;
class Identity
{
protected:
char *name;
int dob;
char* blood_group;
Identity(char *iname="", int nlength=1, int idob=0, char *iblood_group="", int blength=2):dob(idob)
{
name = new char[nlength+1];
strncpy(name,iname,nlength);
name[nlength]=' ';
blood_group = new char[blength+1];
strncpy(blood_group,iblood_group,blength);
blood_group[blength]=' ';
}
~Identity()
{
delete[] name;
delete[] blood_group;
}
};
class Physical
{
protected:
double height;
double weight;
Physical(double pheight = 0.0, double pweight = 0.0):height(pheight),weight(pweight)
{
}
};
class Registration
{
protected:
int policy_number;
char* contact_address;
Registration(int p_num=0, char* addr="", int alength=1):policy_number(p_num)
{
contact_address = new char[alength+1];
strncpy(contact_address,addr,alength);
contact_address[alength] = ' ';
}
~Registration()
{
delete[] contact_address;
}
};
class Contact:public Identity, public Physical, public Registration
{
private:
char* ph_number;
char* driver_license;
public:
Contact(char *name ="",int nlength=0,int dob = 0, char* blood = "", int blength = 0,double height = 0, double weight = 0, int pol_num = 0, char* cont_addr="", int alength=10, char *ph="",int plength=10,char* lic="",int llength=10):Identity(name,nlength,dob,blood,blength),Physical(height,weight), Registration(pol_num,cont_addr,alength), ph_number(ph),driver_license(lic)
{
ph_number = new char[plength+1];
strncpy(ph_number,ph,plength);
ph_number[plength] = ' ';
driver_license = new char[llength+1];
strncpy(driver_license,lic,llength);
driver_license[llength] = ' ';
}
~Contact()
{
delete[] ph_number;
delete[] driver_license;
}
char* GetName()
{
return name;
}
int GetDob()
{
return dob;
}
char* GetBloodGroup()
{
return blood_group;
}
double GetHeight()
{
return height;
}
double GetWeight()
{
return weight;
}
int GetPolicyNum()
{
return policy_number;
}
char* GetAddress()
{
return contact_address;
}
char* GetPhoneNumber()
{
return ph_number;
}
char* GetDriverLicense()
{
return driver_license;
}
};
int main()
{
using namespace std;
Contact kck("MyName",strlen("MyName"),11111111,"A+",strlen("A+"),15.10,651.5,1111,"MyArea",strlen("MyArea"),"1111111111", strlen("1111111111"),"ABCD1234",strlen("ABCD1234"));
cout << kck.GetName() << endl;
cout << kck.GetDob() << endl;
cout << kck.GetBloodGroup() << endl;
cout << kck.GetHeight() << endl;
cout << kck.GetWeight() << endl;
cout << kck.GetPolicyNum() << endl;
cout << kck.GetAddress() << endl;
cout << kck.GetPhoneNumber() << endl;
cout << kck.GetDriverLicense() << endl;
return 0;
}
在许多地方,您可以编写以下内容:
char *name = ""
但是,""
具有类型const char[1]
。这隐式地转换为const char *
。但是,您尝试将其分配给char *
,这是试图忽略const
限定符。
由于C++11,这是完全不允许的。在C++11之前,这是允许的,但不推荐使用。编译器警告你这是个坏主意。
如果您真的必须使用指针,那么在任何可能指向字符串文字的情况下都可以使用char const *
。
但是你最好完全避免使用指针,因为它们会使你的代码复杂化,并引入错误的机会,如果你没有使用指针,就不会出现错误。将字符串的长度与字符串分开传递是非常糟糕的。
例如,使用string
保存所有字符串。作为一个初学者,最好从最简单的技术开始,巧合的是,这也是最好的技术。到目前为止,你编写程序的方式只是无端地让自己的生活变得困难。
相关文章:
- 为什么布尔开关语句有编译器警告?
- 为什么 gcc 会给我可能未初始化的警告 deque::insert 带有过滤范围
- 为什么我会收到警告,指出函数已使用但未定义,以及已定义但未使用?
- 为什么 C++20 中的 [[可能]] 属性在这里引发警告?
- 为什么我会收到此警告:ISO c++ 禁止可变长度数组"v"[-Wvla]
- 为什么此程序显示"unreachable code"警告?我该如何抑制它?
- 为什么在 c++ 中索引字符串会发出隐式转换警告?
- 双重标准?为什么只有 char* const&a = "bla" 的警告?
- 为什么警告阻止我们编写优化程序
- 为什么 gcc 警告只针对统一初始化缩小转换范围?
- 常量更改而不const_cast<> 为什么没有编译器警告/错误?
- 为什么编译器不在同一翻译单元中警告 ODR 违规
- 为什么我会收到此警告? "Member 'x' was not initialized in this constructor"
- 为什么 gcc 和 clang 都没有发出任何警告?
- 为什么我应该始终启用编译器警告
- 为什么 Clang 警告未使用的指针和未使用的基元,而不是未使用的对象?
- 为什么 ++ 运算符触发器返回对局部变量警告的引用
- 为什么[[nodiscard]]只鼓励编译器发出警告,而不要求它?
- 为什么 CLion 给我一个警告?
- C4533警告:为什么goto跳过变量初始化