Thread: constructor to set variable to an empry string

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    constructor to set variable to an empry string

    I am a new to c++, I have to set the variable name to an empty string in the class constructor. This is the way i approached it, is this correct?

    class test
    {
    private:
    char* name;

    prublic:
    Test()
    {
    name[0] = '\0';
    }

    };

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Re: constructor to set variable to an empry string

    Originally posted by wireless
    I am a new to c++, I have to set the variable name to an empty string in the class constructor. This is the way i approached it, is this correct?

    class test
    {
    private:
    char* name;

    prublic:
    Test()
    {
    name[0] = '\0';
    }

    };

    First off, Test() should be test().


    test()
    {
    name = new char[input desired size];
    }

    Is what i'd use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Set string variable
    By Beaner in forum C Programming
    Replies: 3
    Last Post: 01-28-2006, 06:15 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM