initializing a static string

This is a discussion on initializing a static string within the C++ Programming forums, part of the General Programming Boards category; How can we initialize the static string str in class 'a' below.. Code: #include<iostream> #include<string,h> class a { static char ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    initializing a static string

    How can we initialize the static string str in class 'a' below..

    Code:
    #include<iostream>
    #include<string,h>
    
    class a
    {
    static char str[80];
    };
    
    int main()
    {
    
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    You would say( globally ):
    Code:
    char a::str[80] = "This is a string";
    EDIT: Actually, I'm not so sure about what I wrote above. You could declare a public static function that would set the string.
    Last edited by GReaper; 02-28-2012 at 08:25 AM.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    GReaper is correct, but of course this should go in the source file, not the header file.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializing static stdlib containers
    By skewray in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2008, 07:23 PM
  2. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 05:45 AM
  3. Initializing a Bit string
    By sarathius in forum C Programming
    Replies: 2
    Last Post: 02-11-2008, 07:09 AM
  4. Initializing static const memebers in a class
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2007, 06:39 PM
  5. Initializing a C String
    By cpluspluser in forum C++ Programming
    Replies: 3
    Last Post: 04-10-2003, 12:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21