C++ 在C+;中使用字符串作为参数或返回类型的正确方法是什么+;

C++ 在C+;中使用字符串作为参数或返回类型的正确方法是什么+;,c++,string,arguments,C++,String,Arguments,在这段代码中,方法名为setname()。 在这个方法中,我使用参数(Char*aname),参数“Name_Student”(调用时) 但获取错误:“不推荐将字符串常量转换为'char*'[-Wwrite strings]” 当我将这个字符串存储在一个数组中并像 char name_s[] = "Name_Student"; .setname(name_s); // working fine 您应该在“char*”之前插入“const”,但是这里已经回答了这个问题:可能您要查找的内容已经得到

在这段代码中,方法名为setname()。 在这个方法中,我使用参数(Char*aname),参数“Name_Student”(调用时)

但获取错误:“不推荐将字符串常量转换为'char*'[-Wwrite strings]”

当我将这个字符串存储在一个数组中并像

char name_s[] = "Name_Student";
.setname(name_s); // working fine

您应该在“char*”之前插入“const”,但是这里已经回答了这个问题:

可能您要查找的内容已经得到了回答

下面是对我在问题下方发布的关于字符串的评论的回复:

在C/C++和C/C++中,您可以选择是通过引用传递还是通过值传递。有两种方法可以做到这一点-操作符*(指针指向)和操作符&(地址)。但是,在您的情况下,使用传递值(而不是引用)完全可以:

#include <iostream>
#include <string>

using namespace std;

class student
{
    int rollno;
    string name;
    public:
        void setname(string name) { this->name = name; }
        void setrollno(int rollno) { this->rollno = rollno; }
        string getname() { return name; }
        int getrollno() { return rollno; }
};

int main()
{
  student astudent;
  astudent.setname("Name_Student"); // here you pass by value
  astudent.setrollno(10);
  astudent.getname();               // name is set, so everything is okay :)
  astudent.getrollno();
  return (0);
}
然而,在这种情况下

astudent.setname("Name_Student");
不可能,您必须先创建对象,然后将其传递给函数:

string newName = "Name_Student";
astudent.setname(newName);
如果您想在类之外更改名称,这很有用,但是这可能会被视为违反OOP原则的行为,因为更改对象的内容只能通过它所代表的类严格定义的例程来完成,而不仅仅是这样

下面是代码的重做,用一个小示例来说明我的意思:

#include <iostream>
#include <string>

using namespace std;

class student
{
    int rollno;
    string* name;  // very important! otherwise you will just store a COPY of the new name instead of a reference to the object that was passed to setname()
    public:
        // here we pass by reference a string object!
        void setname(string& name) { this->name = &name; }
        void setrollno(int rollno) { this->rollno = rollno; }
        string getname() { return *name; }
        int getrollno() { return rollno; }
};

int main()
{
  student astudent;
  string str = "Name_Student";

  astudent.setname(str);        // we pass by reference here!
  cout << astudent.getname() << endl;
  str += "LALALA";              // because we have passed the string object by reference to the astudent object we can change its contents outside the class without using the setter provided by the class
  cout << astudent.getname() << endl;

  return (0);
}

再说一次,就你的情况而言,我认为你不需要任何旁敲侧击。还有一些建议——尝试在C++编程时使用C++提供的特性。当然,在某些情况下,您可能希望坚持旧的char*方式来处理您的案例,但这只是糟糕的编码风格std::string还提供了c_str()函数,该函数返回一个c字符串(有关详细信息,请参阅)。

除非您有特定原因使用
char*
否则您应该使用
std::string
。我认为使用
std::string*
的想法很糟糕,因为学生名字的生命周期与学生对象是分开的。考虑下面的代码:

student get_student()
{
    // get the student from the database
    string name = "some name";
    int rollno = 1;
    student s;
    s.setname(name);
    s.setrollno(rollno);
    return s;
}
如果你尝试使用学生的名字,你将引用一个不再存在的对象。您可以尝试以下方法:

student s = get_student();
cout << s.getname() << endl;
如果
setname
将参数作为
const字符串&
使用,则仍然可以使用
setname(“Name”)
,因为这将创建一个可以绑定到const引用的右值

int main(int argc, char *argv[]) {
    student astudent("Student's Name", 10);
    cout << "Roll #: " << astudent.getrollno() << '\n';
    cout << "Name: " << astudent.getname() << '\n';
    astudent.setname("New name");
    cout << "Name: " << astudent.getname() << '\n';
    string n{"Yet Another Name"};
    astudent.setname(n);
    cout << "Name: " << astudent.getname() << '\n';

    return (0);
}
intmain(intargc,char*argv[]){
学生姓名(“学生姓名”,10);

我看不出您已经导入了标题。为什么不使用std::string而不是char?在您的代码中,我看不出为什么更喜欢char*而不是std::string。谢谢回复所有人。@rbaleksandar,我认为当我使用std::string时,它是按值传递的(需要确认).但这里我用的是指针。我想知道当我编写代码时我应该做什么,不应该做什么,为什么不做。这样我在编写代码时就有信心了。我决定发布回复(不是评论)因为它需要更多的解释,而且注释中不存在的代码格式是个麻烦。感谢您的注释…我更感兴趣的是“为什么”不“如何修复它”我已经有了2个解决方案…在这一页上,我得到了这个[任何您传递字符串文本的函数”我是字符串文本“应使用char const*作为类型,而不是char*]因此,任何人都可以回答,如果类型现在使用的是'const',在施法时,我没有使用仅使用'char*'的'const',为什么它会起作用?我只想用简单的话来理解。我在开始时用一个链接更新了我的答案,指向可能足以让您理解“为什么”的内容谢谢你的回答,抱歉,格式不好的代码。是的,你建议OOPS的场景也有PASBYEVE和PassByRef,以及如何使用String。是的,你建议“在C++编程时尝试使用C++提供的特性”。我会遵循。没问题,很高兴能帮助你。^ ^ ^
Name_Student
Name_StudentLALALA
student get_student()
{
    // get the student from the database
    string name = "some name";
    int rollno = 1;
    student s;
    s.setname(name);
    s.setrollno(rollno);
    return s;
}
student s = get_student();
cout << s.getname() << endl;
class student {
    int rollno;
    string name;
    public:
        student(const string& name, int rollno): name(name), rollno(rollno) {}
        void setname(const string& name) { this->name = name; }
        void setrollno(int rollno) { this->rollno = rollno; }
        const string& getname() { return name; }
        int getrollno() { return rollno; }
};
int main(int argc, char *argv[]) {
    student astudent("Student's Name", 10);
    cout << "Roll #: " << astudent.getrollno() << '\n';
    cout << "Name: " << astudent.getname() << '\n';
    astudent.setname("New name");
    cout << "Name: " << astudent.getname() << '\n';
    string n{"Yet Another Name"};
    astudent.setname(n);
    cout << "Name: " << astudent.getname() << '\n';

    return (0);
}