-
March 13th, 2009, 06:30 AM
#1
what this line of code means?
hello,
I am a noob in C++ and need help about the bellow line of code:
typedef basic_string<char, char_traits<char>, allocator<char> > string;
what the above line of code means ? what it does?
Last edited by soroush_vs; March 13th, 2009 at 06:33 AM.
-
March 13th, 2009, 06:46 AM
#2
Re: what this line of code means?
It declares a typedef of type basic_string<char, char_traits<char>, allocator<char> > named string. What specific aspect of it do you want to know about?
-
March 13th, 2009, 06:47 AM
#3
Re: what this line of code means?
It defines a type under the name string. That type is an instantiation of the basic_string class template parameterized by types char, chart_traits and allocator (the last two are also class templates parameterized by type char).
Basically, you need knowledge of C++ templates and the typedef keyword in order to understand that. Check them in your favorite C++ book or on the internet.
-
March 13th, 2009, 08:06 AM
#4
Re: what this line of code means?
small point but typedef does not define a type. classes and structs define types. A typedef creates an alias for a type so you can refer to a long-windedly-named-type by its alias.
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
March 13th, 2009, 02:48 PM
#5
Re: what this line of code means?
Originally Posted by Mybowlcut
It declares a typedef of type basic_string<char, char_traits<char>, allocator<char> > named string. What specific aspect of it do you want to know about?
Thanks All.
I want to know exactly what happens when we define a variable with type "basic_string<char, char_traits<char>, allocator<char> >" or simply "string".
I what to know this line :>>> basic_string<char, char_traits<char>, allocator<char> >
Last edited by soroush_vs; March 13th, 2009 at 02:53 PM.
-
March 13th, 2009, 03:06 PM
#6
Re: what this line of code means?
That's exactly what you said before. You haven't elaborated any on your confusion.
Originally Posted by Russco
small point but typedef does not define a type. classes and structs define types. A typedef creates an alias for a type so you can refer to a long-windedly-named-type by its alias.
Technically, it is a definition (as opposed to a declaration).
-
March 14th, 2009, 12:23 AM
#7
Re: what this line of code means?
I want to know what this data type store data .How this string store information and how we can manipulate it?
I want to know why we use this instead of a simple char or TCHAR or CString or other simplest forms.
I what to know why this defines at this such a form : basic_string<char, char_traits<char>, allocator<char> >
I what to know this line of code means that string is really a char or char_traits or allocator or something other?
Code:
typedef basic_string<char, char_traits<char>, allocator<char> > string;
How compiler deal with it?When compiler goes into the source code and saw a "string" then change it into basic_string<char, char_traits<char>, allocator<char> > then it means that this is a data type? like this?
Code:
string str1;
basic_string<char, char_traits<char>, allocator<char> > str2;
what are these str1 and str2 exactly ?
-
March 14th, 2009, 02:22 AM
#8
Re: what this line of code means?
Originally Posted by soroush_vs
I want to know what this data type store data .How this string store information and how we can manipulate it?
Get a C++ book and read about std::string.
I want to know why we use this instead of a simple char or TCHAR or CString or other simplest forms.
A char is a single character. A TCHAR is also a single character and it only works for Windows. A CString only works for MFC. What if I don't work in Windows, and I need a string? Use std::string.
I what to know why this defines at this such a form : basic_string<char, char_traits<char>, allocator<char> >
Because a std::string is really a template.
I what to know this line of code means that string is really a char or char_traits or allocator or something other?
Get a book on C++ and templates. A basic_string<> is a general template class that handles sequences of characters. A character can be an 8 bit character, 16-bit character, or any type that works. A sequence of char types is a basic_string<char>.
The char_traits<char> is another template that tells the basic_string<> template how to handle the "char" in certain places in the basic_string template function.
The allocator template tells how to allocate memory when the basic_string template asks for memory.
Now it's your turn to do the research and learn C++, as all of these questions are answered in depth and in full in C++ books, and by learning the C++ language. Randomly just looking at C++ source code, and then asking questions is not the way to learn the language.
Code:
string str1;
basic_string<char, char_traits<char>, allocator<char> > str2;
what are these str1 and str2 exactly ?
Variables.
Regards,
Paul McKenzie
-
March 14th, 2009, 03:33 AM
#9
Re: what this line of code means?
Why you need the underlying of C++ string template ? I think you better grasp basic of C++ first.
Thanks for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|