0

I have a structure with 2 fields which have a value. I would like to implement a method that changes the values of the fields. I did this with the following code, but it is not correct. I would like to ask how can I fix this?

#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;
}
3
  • Please use std::string rather than char *. Commented Sep 19, 2018 at 16:13
  • I think, you must look at the C++ Data Structures. I recommend read this blog Commented Sep 19, 2018 at 16:17
  • @G.M.: Sometimes an std::string_view is more appropriate. Commented Dec 9, 2018 at 12:42

4 Answers 4

1

A struct works like a class, the only difference is that all inside the struct is public by default.

So you can (and in this case you have to) declare setName inside the struct

Sign up to request clarification or add additional context in comments.

Comments

0

Something like:

void SetName(CPerson& p, const char* szName) {
    p.name = szName;
}

Be careful though, szName argument may be pointing to an array whose lifetime may end before the lifetime of CPerson::name ends. That makes CPerson::name an invalid pointer.

3 Comments

name is not defined in SetName. Needs to be a member function
However it says identifier name (from the structure) is not defined? I want to change the name field of the declared strucuture
@A.Dimitrov You need to pass Person object into the function.
0

you need to make it a member function

#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;
}
};

that code is still wrong as its just juggling pointers. You should use std::string to store strings. But at least the code will compile;

1 Comment

i'm not the downvoter but I think trying to change a const pointer once it has been initialized may not work so well.
0

I would like to implement a method that changes the values of the fields.

I wouldn't be so certain you should do that.

You see, the fields of a struct are, by default, publicly-accessible - which means anyone can change them, method or no method. Why bother writing such a method? Isn't it redundant?

If you had a class instead of a struct, or if you had set your field access to protected or private, then it makes sense to have a method for setting field value, e.g.:

#include <string>
#include <string_view>

struct Person {
protected:
    std::string name = "Ivan";
    std::string gender = "Male";

public:
   void changeName(std::string_view new_name) {
       name = new_name;
   }
};

(string_view is new in C++17; before that, you could use const std::string&).

But even then it's not always a good idea to have that. Also, in this struct of yours, there's a default name and gender, without a good reason. Instead of both these things, you might make the struct immutable:

#include <string>

struct Person {
    const std::string name;
    const std::string gender;
};

which, for many applications, makes sense (e.g. if you get this from some database, rather than tracking a person's name and gender throughout their lives).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.