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

This is a discussion on Any way to simplify this string return in my member function? within the C++ Programming forums, part of the General Programming Boards category; This is my member function to take the value and suit of a card and return a single string for ...

  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
    22,119
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    22,901
    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,528
    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 in reply to you, it is likely you deserve it. Suck it up, sunshine, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    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, 05: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, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21