VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 289|回复: 0

const char*以及char*区别,string类型是否能够在strcpy函数中使用

[复制链接]
52_avatar_middle
最佳答案
0 
在线会员 发表于 2022-6-28 04:49:26 | 显示全部楼层 |阅读模式
两个问题:
第一个问题:const char*与char*的区别(结合下面给出的例子),我清楚const表示的就是静态的,但是在下面给出例子中,student.cpp文件的Student::Student(int id, const char* pname, char sex, int age) 不能写为Student::Student(int id, char* pname, char sex, int age) ,给出的错误提示是因为test.cpp的Student stu2(1,"zhangsan",'m',21)这种表达不能与该构造函数相匹配
第二个问题::那个把属性name改为string类型,而不是name的char类型的数组,
当然构造函数也改变Student::Student(int id, const char* pname, char sex, int age),形参改为string类型,为什么不能使用用strcpy函数,错误提示原因是string类型与const char *类型不能转换过来
//student.cpp

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


Student::Student() {
        id = 0;
        memset(name, 0, 50);
        age = 10;
        sex = 'm';
}
Student::Student(int id, const char* pname, char sex, int age) {
        this->id = id;
        strcpy(name,pname);
        //memset(name, 0, 50);
        this->age = age;
        this->sex = sex;
}

//student.h
#define _CRT_SECURE_NO_DEPRECATE
#pragma once
//#pragma warning(suppress : 4996)
//#pragma warning(disable : 4996)
class Student
{
public:
        int id;
        char name[50];
        char sex;
        int age;
        Student();
        Student(int id,const char* pname,char sex,int age);
};
//test.cpp
#include <iostream>
#include "Student.h"
using namespace std;

int main() {
        Student stu1;
        Student stu2(1,"zhangsan",'m',21);
        return 0;
}





上一篇:ADO打开SQL SEVER 2012 出错
下一篇:QT问题:界面在运行,但不显示
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

×【发帖 友情提示】
1、请回复有意义的内容,请勿恶意灌水;
2、纯数字、字母、表情等无意义的内容系统将自动删除;
3、若正常回复后帖子被自动删除,为系统误删的情况,请重新回复其他正常内容或等待管理员审核通过后会自动发布;
4、感谢您对VC驿站一如既往的支持,谢谢合作!

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号-4 )|网站地图wx_jqr

GMT+8, 2023-5-29 23:09

Powered by CcTry.CoM

© 2009-2021 cctry.com

快速回复 返回顶部 返回列表