Thread: Any way to simplify this string return in my member function?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    37

    Any way to simplify this string return in my member function?

    This is my member function to take the value and suit of a card and return a single string for both variables. (first year of college, go easy on me!) Is there a simpler way to do this? I tried to just return value + suit but I get a conversion error, and its probably more code to convert their type.

    Code:
     
    string PlayingCard::getCardCode()
    {
        string cardCode;
    
        cardCode = value; // value is a char defined in the Class header
        cardCode += suit; // same for suit, both are defined in the default constructor and set to '0'
    
        return cardCode;
    }
    Last edited by Striph; 01-21-2012 at 12:58 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you just want something concise, then:
    Code:
    string PlayingCard::getCardCode() const
    {
        return string(1, value) + suit;
    }
    Note that the declared the member function const since it does not change the observable state of the PlayingCard.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    37
    Interesting, I haven't learned the const after the function method yet. I noticed that it does compile without the const, and also with the const on the end of the member function when I put it in the class declaration.

    I assume specifying const like that prevents the getCardCode method from affecting the original values in the class?

    Thanks for the reply, learned something new today!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Striph View Post
    I assume specifying const like that prevents the getCardCode method from affecting the original values in the class?
    Yep.
    Prevents the method from modifying any variables stored in the class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    Yep.
    Prevents the method from modifying any variables stored in the class.
    You left out "non-mutable"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I did, but I did it on purpose. No need to confuse a newbie.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simplify recursive function
    By MattJ812 in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2011, 05:31 PM
  2. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  3. String member function find error
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2005, 06:51 PM
  4. Getting a function to return a string
    By Necrodeemer in forum C++ Programming
    Replies: 5
    Last Post: 01-17-2003, 07:43 PM
  5. member function to return obect
    By rip1968 in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 10:51 PM