Firstly: when copying a string, DO NOT just say:
1 2
|
char* a="Hello";
char* b=a;
|
Instead, use strcpy:
1 2 3 4
|
#include <cstring>
char* a="Hello";
char* b;
strcpy(b,a); // strcpy(char* dest, char* source) copies the string source into dest
|
Second:
char *&
is an example of a
reference - in this case a reference to a char*. When you pass a value to a function, eg
1 2 3 4 5 6 7 8 9
|
int myFunc (int a)
{
a+=5;
return a;
}
int y=5;
int x=myFunc(y);
cout << x << "\n" << y;
|
the function gets given a copy of y - so if it changes its copy it doesn't change y. However, if myFunc was passed a reference: eg
1 2 3 4 5 6 7 8 9
|
int myFunc (int& a)
{
a+=5;
return a;
}
int y=5;
int x=myFunc(y);
cout << x << "\n" << y;
|
the function gets given a reference to y - when it does anything to its reference, that thing is happening to y. So when it calls a+=5, what's really happening is y+=5
In your case (using Album::get_record_label as an example), char *& label means that you are being given a reference to the variable that will store a copy of record_label which the user can then work with. What the function should do is
1 2 3 4 5 6
|
bool Album::get_record_label (char*& label)
{
if (/* record_label is not set */) return false;
strcpy(label,record_label);
return true;
}
|
(edit):
Third: Your constructor should set all the data members to values which noone will ever use - set all the char*'s to NULL and the int's to -1, for example.
Then, if one of the set_"" functions tries to set a char* to NULL or an int to -1, it returns false, or if one of the get_"" tries to get a value which hasn't been set, it returns false.
That's why most of the functions return bool