C++ style strings

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

C++ style strings

Postby darkspork » Sun Nov 01, 2009 7:24 pm UTC

Teacher failed to explain this or provide any USEFUL examples, and Google doesn't seem to know the difference between C and C++. How can I return a string properly from a function?

Code: Select all

String Date::getMonthName() const
{
    switch(this->month)
    {
        case 1: return "January";
        case 2: return "February";
        case 3: return "March";
        ...
        case 12: return "December";
        default: return "Invalid Month";
    }
}


It seems 'String' does not name a type.
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

User avatar
Qoppa
Posts: 694
Joined: Sat Nov 24, 2007 9:32 pm UTC
Location: Yes.

Re: C++ style strings

Postby Qoppa » Sun Nov 01, 2009 7:48 pm UTC

It's not capatalized.

Code: Select all

string Date::getMonthName() const
...

not

Code: Select all

String Date::getMonthName() const
...

Code: Select all

_=0,w=-1,(*t)(int,int);a()??<char*p="[gd\
~/d~/\\b\x7F\177l*~/~djal{x}h!\005h";(++w
<033)?(putchar((*t)(w??(p:>,w?_:0XD)),a()
):0;%>O(x,l)??<_='['/7;{return!(x%(_-11))
?x??'l:x^(1+ ++l);}??>main(){t=&O;w=a();}

User avatar
jaap
Posts: 2084
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: C++ style strings

Postby jaap » Sun Nov 01, 2009 7:50 pm UTC

darkspork wrote:It seems 'String' does not name a type.


But 'string' does - note the lower case letter s.
Actually, it is in the std namespace so you ought to refer to it as std::string, assuming you don't import it with the 'using' command.

User avatar
darkspork
Posts: 532
Joined: Tue Sep 23, 2008 12:43 am UTC
Location: Land of Trains and Suburbs

Re: C++ style strings

Postby darkspork » Sun Nov 01, 2009 7:56 pm UTC

Ah. Isn't the convention to use uppercase letters for classes though?
Shameless Website Promotion: Gamma Energy
My new esoteric programming language: GLOBOL
An experiment to mess with Google Search results: HARDCORE PORNOGRAPHY HARDCORE PORNOGRAPHY

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: C++ style strings

Postby lulzfish » Sun Nov 01, 2009 8:03 pm UTC

darkspork wrote:Ah. Isn't the convention to use uppercase letters for classes though?

Yeah, but not in the STL.
I think most other stuff does, like Qt and Irrlicht... I don't know what the STL' point it. Maybe to blend in with "int" and "bool", which aren't capitalized.

Posi
Posts: 111
Joined: Mon Jul 16, 2007 6:08 am UTC

Re: C++ style strings

Postby Posi » Sun Nov 01, 2009 11:37 pm UTC

darkspork wrote:Ah. Isn't the convention to use uppercase letters for classes though?

The STL is older than the ClassesLikeThis convention, and uses classes_like_this.

On a somewhat related note, does anyone know of a good 3rd party string class? Preferably one like Java's or Qt's? (I don't want to have to include Qt for just a string class.)

User avatar
ash.gti
Posts: 404
Joined: Thu Feb 07, 2008 1:18 am UTC
Location: Probably a coffee shop.

Re: C++ style strings

Postby ash.gti » Mon Nov 02, 2009 12:29 am UTC

Posi wrote:On a somewhat related note, does anyone know of a good 3rd party string class? Preferably one like Java's or Qt's? (I don't want to have to include Qt for just a string class.)

What kind of string stuff? I think boost's libraries cover just about everything you could need, IMO... but then again I may not be doing whatever it is your trying to do.
# drinks WAY to much espresso

Posi
Posts: 111
Joined: Mon Jul 16, 2007 6:08 am UTC

Re: C++ style strings

Postby Posi » Mon Nov 02, 2009 1:51 am UTC

ash.gti wrote:
Posi wrote:On a somewhat related note, does anyone know of a good 3rd party string class? Preferably one like Java's or Qt's? (I don't want to have to include Qt for just a string class.)

What kind of string stuff? I think boost's libraries cover just about everything you could need, IMO... but then again I may not be doing whatever it is your trying to do.

Boost does cover most of it, its just the coding style for string stuff is really... weird.

User avatar
MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

Re: C++ style strings

Postby MHD » Tue Nov 03, 2009 10:18 am UTC

Posi wrote:
darkspork wrote:Ah. Isn't the convention to use uppercase letters for classes though?

The STL is older than the ClassesLikeThis convention, and uses classes_like_this.

On a somewhat related note, does anyone know of a good 3rd party string class? Preferably one like Java's or Qt's? (I don't want to have to include Qt for just a string class.)


classes_like_this are a lot nicer than ClassesLikeThis, IMHO.
Classes_like_this is ok too.
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 11 guests