Thread: STL String and member funcs

  1. #1
    Nix Phoeni
    Guest

    Angry STL String and member funcs

    I'm new to the STL string (all of STL for that matter). I just learned that a NULL string can't be assigned in the copy constructor, and since I'm still getting errors, I'm guessing in the operator = as well. Is there any way to check if I'm going to be assigning a NULL string to another string? I tried

    if(name=="") return;
    else file = name;

    but it didn't really work (name had an address of 0xdddddddd). What can I do? Any help or a website I could find would be appreciated.

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Use the strcmp() function from the <cstring> header. For example:
    Code:
    #include <cstring>
    #include <string>
    using namespace std;
    
    int main()
    {
        string file;
        char* name = "Some string.";
    
        if(strcmp(name, "") == 0)
            return 0;
        else
            file = name;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Unhappy not quite...

    Sorry, I wasn't clear. In this code

    if(name=="") return;
    else file = name;

    both name and file are strings. Is there some way to check if name is NULL?
    -Joe
    -------------------------------------------
    To understand a program you must become both the machine and the program.
    --Alan Perlis

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    To get the length you can use strings length member function -

    if(name.length()==0) return;
    else file = name;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly inherit from template?
    By 6tr6tr in forum C++ Programming
    Replies: 118
    Last Post: 04-25-2008, 04:30 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM