gpt4 book ai didi

c++ - 如何返回类中私有(private)成员字符串的值

转载 作者:行者123 更新时间:2023-11-27 23:07:12 24 4
gpt4 key购买 nike

<分区>

我想获取 sampleclass 中私有(private)字符串 name 的值。

#include <iostream>
#include <string>

using namespace std;

class sampleclass {
public:
int getname(){ //this is my attempted getter
string x = name;
}
private:
string name= "lance"; //this is the private I want returned by value

};

int main(){
sampleclass object;
cout << object.getname();
}

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