CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Posts
    17

    Question 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.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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?
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    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.

  4. #4
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    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.

  5. #5
    Join Date
    Feb 2009
    Posts
    17

    Re: what this line of code means?

    Quote Originally Posted by Mybowlcut View Post
    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.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: what this line of code means?

    That's exactly what you said before. You haven't elaborated any on your confusion.

    Quote Originally Posted by Russco View Post
    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).

  7. #7
    Join Date
    Feb 2009
    Posts
    17

    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 ?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: what this line of code means?

    Quote Originally Posted by soroush_vs View Post
    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

  9. #9
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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
  •  





Click Here to Expand Forum to Full Width

Featured