Skip to main content
GameDev.net gamedev.net
🔒 Locked

Accessor (Getter) function for a c string (character array)

Started by CoryG89 Jun 23, 2010 at 2:22 AM 6 replies 9.8k views
Original Post
CoryG89
CoryG89
Ok, so I am working on this for school. I am building a model business application in the console using C++.

The application is modeled as a database/cashier program. I have a class called BookData that is supposed to hold a number of character arrays and a few other variables.

Here are my private members:

char _isbn[14];
char _title[51];
char _author[31];
char _publisher[31];
char _date[11];
int _quantity;
double _cost;
double _price;


I am supposed to have public accessor and mutator functions that 'get' and set' each private member of the class. I was surprised to learn this, but apparently you cannot return an array from a function in C++

Normally I know I would write an accessor function for the last three similar to something like this:

double BookData::getCost()
{
return _cost;
}

I am not sure how I should go about doing this for the character arrays. This is for a C++ course and I believe I am supposed to keep the c strings and not use strings from the standard libraries. I cannot remember but I think that you are supposed to use pointers to do this somehow. I tried but the syntax seems clumsy and I am getting errors. Any tips would be most appreciated.

This is supposed to work by booting up and filling a dynamically allocated array of BookData class objects from a delimited data file. I need these 'setter' / 'getter' functions for accessing and manipulating these values to add other functionality. I am going to be working on this for the rest of tonight and all tomorrow. So anyone that can give me any advice on this subject would be most helpful.
That which doesn't kill you makes you stronger.
Nypyren
Nypyren
'const char *' is your friend (well, actually it's your worst enemy, but if that's what you need to use then so be it).

There is a function called 'strncpy' which you should probably use.

Getters which return char *s are generally a BAD IDEA with c-style strings. The common pattern in C is to use 'caller allocated buffer' functions that look like this:

bool (or size_t) (or char *) GetWhatever(char *buffer, size_t bufferSize); // where you write the return value into 'buffer'

void SetWhatever(const char *buffer); // where you copy the data in buffer to your array.
taz0010
taz0010
You can return an array by returning it's pointer. Pointers and arrays are basically the same thing in c/c++.

char _title[51];

This declares a pointer to a location in memory that has room for 51 chars. You can write a function to return the pointer like so:

char* getTitle(){
return _title;
}

The object being returned is a pointer to the original memory (i.e. not a copy), so the _title can be modified from outside the class. In c++ it's common to provide const getter methods when a pointer is returned.

const char* getTitle() const{
return _title;
}

This protects the _title array from being changed by making the returned pointer "read only", so it can't be changed via the returned pointer, unless you provide a non-const getter method as well.
The std::string class provides a method just like this called c_str(), which you'll use extensively if you're working with both strings and c style APIs.
CoryG89
CoryG89
Ok I am using these functions for accessors in my .cpp file:

const char* BookData::getIsbn()
{
return _isbn;
}

const char* BookData::getTitle()
{
return _title;
}

This will not compile. I think the syntax when I am using the scope operator plus the pointer syntax is wrong.

I can compile and everything is fine when I go ahead and define my functions in the header file, like so:

const char* getTitle()
{
return _title;
}


but I need it in a separated implementation file using the scope resolution operator and I cannot get it to compile.
That which doesn't kill you makes you stronger.
alvaro
alvaro
This should be common sense, but I'll say it anyway: Post the errors that your compiler is giving you. They may not mean anything to you, but chances are others can understand them. What we cannot do is fix your code without seeing it or the compiler's output.
Zahlman
Zahlman
Quote:
Original post by CoryG89
I am building a model business application in the console using C++.... I have a class called BookData that is supposed to hold a number of character arrays [that represent text]


Hmm? Sorry, I thought you said you wanted to use C++.

Quote:
I am supposed to have public accessor and mutator functions that 'get' and set' each private member of the class.


Oh, you actually want to meet the arbitrary requirements of some programming assignment. Well, that's quite unfortunate, then. Your professor/teacher is probably expecting something very specific that is completely unlike real-world professional programming - or good practice in any sense, for that matter.

That said, the best I can do for you is to have you read through this. The rules here are the same in C++ as they are in C.

EDIT: No, wait, I can do more for you. I can attempt to address your problems with putting code in separate implementation files, and move you to For Beginners.
taz0010
taz0010
Quote:

const char* BookData::getIsbn() const
{
return _isbn;
}

const char* BookData::getTitle() const
{
return _title;
}


Add the parts in bold. Const after the function name means that the function does not change the class's data. So these two functions return pointers that cannot be used to change the title/ISBN, and calling the functions themselves does not change the BookData class in any way.

When you declare the function in a header, you need to provide the matching definition in the source file. This includes name, return value, parameters, and const.
So in the header file you'll have:

const char* getIsbn() const;const char* getTitle() const;


And in the source file:

const char* BookData::getIsbn() const{return _isbn;}const char* BookData::getTitle() const{return _title;}
CoryG89
CoryG89
Original post by taz0010
Quote:

Const after the function name means that the function does not change the class's data. So these two functions return pointers that cannot be used to change the title/ISBN, and calling the functions themselves does not change the BookData class in any way.



Thank you, looks like the last const keyword is what I was missing, thank you for actually helping, and not going on to tell me about how it's unfortunate that I choose to program in a way my teacher requires in order to get a degree o_O.
That which doesn't kill you makes you stronger.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.