Constructor help with const C-string array
I have a simple class Name:
class Name
{ private:
char nameStr[30];
public:
Name(cons开发者_StackOverflow社区t char * = "");
char * getName() const;
void print() const;
};
I am having two problems, which are just a bit difficult for me wrap my head around. The constructor is supposed to take an argument of const char *
and copy that into the nameStr array. However, this is what I have tried and is not working:
Name::Name(const char * setName)
{
&nameStr = setName;
}
It gives me the error "invalid lvalue in assignment". If I remove the reference in front of nameStr, it still errors, saying "incompatible types in assignment of 'const char*' to 'char[30]'". Not quite sure what I'm missing.
The other problem is the accessor method:
char * Name::getName() const
{
return &nameStr[0];
}
I have the same problem, it errors "invalid conversion from 'const char*' to char*'" and I can't for the life of me figure out how to make it return a pointer to a const char... I've been searching to avail. Found plenty of info on const, but can't quite figure how to apply it to my situation. Any help would be greatly appreciated!
The line
char nameStr[ 30 ];
as opposed to (for example)
char * nameStr;
suggests that you want to keep a "copy" of the whole string, not just the pointer. So,
&nameStr = setName;
does not make sense. You can use something like (but may not be exactly)
strncpy( nameStr, setName, 30 );
However, on your second question, why do you want to have the getName()
method return char *
? I see the that the Name
class is encapsulating a char
array, but then if you expose the char
pointer, you are breaking your own encapsulation. If you really want, you can return a const char *
by return nameStr;
, but better is to avoid that.
I would recommend you use an std::string, but first you need to know how pointers work.
Let's take your first problem: &nameStr = setName;
Here, nameStr
is an array of chars, for storing your name. &nameStr
refers to the address of this array. You cannot set the address of a variable of any sort, it is illegal. For example, &a = &b
(where a and b are ints) is just as illegal. Next you tried, nameStr = setName;
and it said incompatible conversion from const char* to char[30]
. What you are trying to do is assign one pointer to another. This doesn't actually copy the data. To copy the data, you need to do this manually, e.g.:
for (int n=0;(n>0?setName[n-1]!=0:true);n++) { nameStr[n] = setName[n]; }
// The above won't work (or might crash) if the ternary operator evaluates both sides. I don't think it does, but correct me if I'm wrong.
Note, the comparion with setName[n-1]
instead of setName[n]
is so the NULL character gets copied.
For your next problem, cannot convert const char* to char*
. Taking the address of a variable will always yeald a constant pointer, because you are not allowed to change the address. Also note that &array[0]
is essentially the same as array
because you a dereferencing, then re-referencing it, thus making the process unnecessary. You need to state your getName() function as returning const char*
and in it, return nameStr
.
A word on arrays: Arrays are like other datatypes, except that they contain multiple of a datatype, not one. When referring to an array, its type is of a pointer to its member type. For example, an array of ints would be treated like an int*
. The pointer points to it's first element. This means that using [] on an array will dereference the pointer to any of its elements.
Good luck with C++!
When you declare something like char nameStr[30]
inside a class or struct, it creates 30 contiguous characters in memory inside the class. The value of nameStr
is a constant pointer to that exact position inside the class, so you can't change it. You'll have to use something like
strncpy(nanemStr, setName, 30)
.
This copies the contents of the string and adds a null terminator. It is limited to 30 characters so you don't overflow the buffer and destroy the universe.
If you are open to using std::string you could do the following:
#include <string>
class Name
{ private:
std::string nameStr;
public:
Name(std::string = "");
std::string getName() const;
void print() const;
};
Name::Name(std::string setName)
{
nameStr = setName;
}
std::string Name::getName() const
{
return nameStr;
}
To clarify what the error message said, the statement &nameStr = setName;
literally says "Set the address of nameStr to the value stored in the variable setName". You can never change the value of a variable's address; that is a constant thing that gets assigned when the variable is defined. Because of that, &nameStr
is as the compiler said, an invalid lvalue.
First problem -
However, this is what I have tried and is not working:
Name::Name(const char * setName)
{
&nameStr = setName;
}
It gives me the error "invalid lvalue in assignment".
Because &nameStr
gives you the address of nameStr
and you can't assign a value to it, it can not be used as lvalue in any of the assignment statement.
If I remove the reference in front of nameStr, it still errors, saying
"incompatible types in assignment of 'const char*' to 'char[30]'"
Because nameStr
is an array of characters and name of the array in C++ is an immutable pointer to its first element. That means you can't change the location where it points to.
Therefore, probably you should try something like this -
strcpy(nameStr, setName);
Second Problem -
char * Name::getName() const
{
return &nameStr[0];
}
Again, the same reason - &nameStr[0]
is still equivalent to writing simply nameStr
.
Try changing your function signature like -
const char * Name::getName() const
精彩评论