warning: deprecated conversion from string constant to 'char*

#include<iostream>
using namespace std;
class Student
{
private:
int age;
char*name;
public:
Student(int m, char *n)
{
age=m;name=n;
}
Student()
{
age=;name="unnamed";
}
~ Student(){}
void SetMember ( int m,char *n )
{
age=m;name=n;
}
int Getage(){return age;}
char *Getname(){return name;}
};
int main()
{
Student stu[]={Student(,"wang"),Student(),Student()} ; stu[].SetMember(,"zhang"); cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
cout<<stu[].Getage()<<","<<stu[].Getname()<<endl;
return ;
}

 

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
class Student {
private:
    int age;
    const char*name;
public:
    Student(int m, const char *n) {
        age=m;
        name=n;
    }
    Student() {
        age=0;
        name="unnamed";
    }
    ~ Student() {}
    void SetMember ( int m,const char *n ) {
        age=m;
        name=n;
    }
    int Getage() {
        return age;
    }
    const char *Getname() {
        return name;
    }
};
int main() {
    Student stu[3]= {Student(13,"wang"),Student(),Student()} ;
 
    stu[2].SetMember(12,"zhang");
 
    cout<<stu[0].Getage()<<","<<stu[0].Getname()<<endl;
    cout<<stu[1].Getage()<<","<<stu[1].Getname()<<endl;
    cout<<stu[2].Getage()<<","<<stu[2].Getname()<<endl;
    return 0;
}

看你的实现,传给Student类的字符串都是不可变的,都加上const就好了;否则你就要复制一份并且自己管理那块内存了。

warning: deprecated conversion from string constant to 'char*的更多相关文章

  1. deprecated conversion from string constant to ‘char*’

    deprecated conversion from string constant to ‘char*’ #include <iostream> using namespace std; ...

  2. warning:deprecated conversion from string constant to 'char *' 解决方案

    #include <iostream> using namespace std; int fuc(char *a) { cout << a << endl; } i ...

  3. warning:deprecated conversion from string constant to &#39;char *&#39;

    warning:deprecated conversion from string constant to 'char *' 解决方式 #include <iostream> using ...

  4. warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]

    在C++中, char* p = "abc"; // valid in C, invalid in C++ 会跳出警告:warning: ISO C++ forbids conve ...

  5. warning: ISO C++ forbids converting a string constant to 'char*'

    第1种字符串赋值方式: char * fileName="./2017-09-02-10-34-10.xml";//这一种字符串赋值方式已经被ISO禁止了 第2种字符串赋值方式: ...

  6. ISO c++11 does not allow conversion from string literal to 'char*'

    http://stackoverflow.com/questions/9650058/deprecated-conversion-from-string-literal-to-char

  7. 将string转换成char*

    string 是c++标准库里面其中一个,封装了对字符串的操作  把string转换为char* 有3中方法:  1.data  如:  string str="abc";  ch ...

  8. expected declaration specifiers or '...' before string constant

    /work/platform_bus_dev_drv/led_dev.c:52: error: expected declaration specifiers or '...' before stri ...

  9. Constant Pool和String Constant Pool详解

    Constant Pool常量池的概念: 在讲到String的一些特殊情况时,总会提到String Pool或者Constant Pool,但是我想很多人都不太明白Constant Pool到底是个怎 ...

随机推荐

  1. leetcode146周赛-1131-绝对值表达式的最大值

    题目描述: class Solution: def maxAbsValExpr(self, arr1, arr2) -> int: def function(s1,s2): result1=[] ...

  2. leetcode-77-组合-字典序

    题目描述: 第一次提交: class Solution: def combine(self, n: int, k: int) -> List[List[int]]: res = [] def b ...

  3. CSS - 选择器相关

    1. 标签选择器 /* 标签选择器 : 会将样式作用在当前网页所有的指定标签上 标签名 { 样式名1: 样式值1; 样式名2: 样式值2; ...... } */ table { width: 300 ...

  4. 【源码】PyObject_VAR_HEAD 定长对象 变长对象

    PyObject_VAR_HEAD      Python-3.7.4\Include\object.h   /* PyObject_VAR_HEAD defines the initial segm ...

  5. 杂项-DTO:DTO(数据传输对象)

    ylbtech-杂项-DTO:DTO(数据传输对象) 数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统.数据传输目标往往是数据访问对象从数据 ...

  6. Android之divider分割线的使用

    1.divider分割线 三种实现方式:(1)添加一个view,(2)通过shape实现,(3)通过设置图片实现 相关属性:设置分割线,分割线位置(none(无),begining(开始),end(结 ...

  7. json-lib解决死循环

    1.添加lazy=“false”(两个xml文件中都要添加) 2.设置过滤器

  8. 0617Python-介绍、三种运行方式、变量、标识符和关键字、获取属性

    一.什么是自动化测试? 1.定义 自动:让机器自己动,就是自动 自动化:让机器按照人类的要求,把软件的所有功能遍历一遍 2.传统测试和自动化测试的区别 传统测试:繁琐.机械.门槛低.工资低 自动化测试 ...

  9. mysql TIMESTAMP 不能为NULL

    一般建表时候,创建时间用datetime,更新时间用timestamp.这是非常重要的. 我测试了一下,如果你的表中有两个timestamp字段,只要你更新任何非timestamp字段的值,则第一个t ...

  10. springboot拦截器的拦截配置和添加多个拦截器

    在spring2.0之前的版本大部分都采用extends WebMvcConfigurerAdapter,把拦截器配置成一个bean,具体的方法,我不细说,网上一大堆.而在spring2.0之后,这个 ...