• Advertisement
Sign in to follow this  

Setting up Mutators 4 a string varible from class

This topic is 4586 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey, i was just wondering how you could set up mutators for a string varible: I have integers set:
//////Set & get Rank:////////////////////////////////////////////////////////////////

soldier::getRank()
{
	return m_Rank;													// returns m_Rank to the caller
}

void soldier::setRank(int rank)
{
	m_Rank = rank;													// sets the rank

}
okay thats real easy, but now i have come 2 strings and was wondering how you could do the same..
soldier::getEmailAddress()
{

	return m_EmailAddress;
}
I need something like this but its declared as a string so return doesnt work,, nehelp would be much appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
All functions in C++ (or C) have a return type. The default happens to be int, but you can specify anything that's a legal type name in the current context (and to avoid confusing people, you should explicitly specify an int return type when that's what you mean). So you would just say "std::string soldier::getEmailAddress() {", and away you go. (You ARE using std::string for your strings, yes? ;) )

However, consider that accessors and mutators are often not useful or even a good idea. Usually they are hiding a design problem, namely the problem of having other objects do work that the object itself should be doing. (Oh, and why would soldiers have email addresses, anyway?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Zahlman
Oh, and why would soldiers have email addresses, anyway?


So that we can all send them happy letters praying that they survive.

...uh, I mean...Accessors/Mutators are the enemy! You are letting the terrists win!

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
All functions in C++ (or C) have a return type. The default happens to be int, but you can specify anything that's a legal type name in the current context (and to avoid confusing people, you should explicitly specify an int return type when that's what you mean). So you would just say "std::string soldier::getEmailAddress() {", and away you go. (You ARE using std::string for your strings, yes? ;) )

However, consider that accessors and mutators are often not useful or even a good idea. Usually they are hiding a design problem, namely the problem of having other objects do work that the object itself should be doing. (Oh, and why would soldiers have email addresses, anyway?)



Thankz a million man.
umm got another problem :)

std::string setEmailAddress(std::string EmailAddress)
{
m_EmailAddress = EmailAddress;
return(1);

}

Some reason it wants a return value, but i dont want 2 give it 1, is there neway i can make it void?

Also i fixed the getEmailAddress like u said, but it returns nothing???
like i call it to get the email address but it doesnt return it

thankz for ur help so far.
I have been taught, that varibles needed in a class have 2 be private, and you then need to provide mutators to access them.

[Edited by - Twinsen2 on July 31, 2005 2:19:30 AM]

Share this post


Link to post
Share on other sites
std::string setEmailAddress(std::string EmailAddress)
^^^^^^^^^^^

You specified quite clearly that you wanted this function to return a string, so you need to return one. Or tell it that you don't want to return anything by changing the return type to void instead.

Share this post


Link to post
Share on other sites
You can give a function a return type of any valid type, but the type you actually do return has to match the type you say you will return.

"void" is a perfectly valid return type, and is what you should use if there is no good reason to return anything. Meanwhile, if there is something that should be returned, you need to, well, return it.


std::string soldier::getEmailAddress() {
// we want to grab some data, so we'll handle that via the return value.
return m_EmailAddress;
}

void soldier::setEmailAddress(const std::string& EmailAddress) {
// we'll do some work on the current soldier, but there's nothing to return.
m_EmailAddress = EmailAddress;
}


Note the use of a "const reference" for the set function; this avoids the implicit copying of the string that would otherwise happen when calling the function. That copying would be redundant since we copy anyway when setting the value. As a general rule, you should pass objects (class instances) this way, and primitives (int, etc.) normally (i.e. by value). In the return for the get, we return by value, in order to copy the data out.

But again, please, rethink your design.

Share this post


Link to post
Share on other sites
You should always specify a return type for functions in C++. This is not just a style thing either, it has been deprecated for some time and section 7.1.5 in the current C++ standard specifies the banning of implicit int.

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
std::string soldier::getEmailAddress() const {
...
}


Good catch. May as well introduce const correctness while the opportunity is there :)

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
std::string soldier::getEmailAddress() const {
...
}


cheerz...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement