gpt4 book ai didi

c++ - 在 C++ 中更改结构内部元素的方法?

转载 作者:行者123 更新时间:2023-11-28 04:32:54 25 4
gpt4 key购买 nike

我有一个包含 2 个具有值的字段的结构。我想实现一种更改字段值的方法。我用下面的代码做了这个,但它不正确。我想问一下我该如何解决这个问题?

#include <iostream>
using namespace std;
struct CPerson {
const char* name = "Ivan";
const char* gender = "Male";

};
void SetName(const char* szName) {
//what to write here?
*name = *szname;
}

最佳答案

一个结构体就像一个类,唯一的区别是结构体内部的所有内容默认都是公共(public)的。

所以你可以(在这种情况下你必须)在结构中声明 setName

关于c++ - 在 C++ 中更改结构内部元素的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52410149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com