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,803
    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 07:25 AM.
    Devoted my life to programming...

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    GReaper is correct, but of course this should go in the source file, not the header file.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 06:23 PM
  2. initializing static members
    By steve1_rm in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2008, 04:45 AM
  3. Initializing a Bit string
    By sarathius in forum C Programming
    Replies: 2
    Last Post: 02-11-2008, 06:09 AM
  4. Initializing static const memebers in a class
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2007, 05:39 PM
  5. Initializing a C String
    By cpluspluser in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2003, 11:34 PM

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