Use static after class definition?

Discussion in 'C++' started by Immortal Nephi, Apr 11, 2010.

  1. I sometimes put static keyword after class definition. Is static
    safe to use with iostream classes? I need to place it in the function
    body. The string in the function body stays in memory for lifetime
    until program terminates.

    enum Ereport_Behavior
    {
    eEnter,
    eTrace,
    eExit
    };

    void Trace( const Ereport_Behavior eReport_Behavior,
    const string strText )
    {
    static ostringstream ossText;

    if( eReport_Behavior == eEnter )
    {
    ossText.str( “” );
    ossText << “Entering…\n”;
    }
    else if( eReport_Behavior == eTrace )
    ossText << strText;
    else if( eReport_Behavior == eExit )
    {
    ossText << “Exiting…\n\n”;
    cout << ossText.str();
    }
    }

    int main()
    {
    Trace( eEnter, “” );
    Trace( eTrace, “Testing Trace()…” );
    Trace( eExit, “” );

    Return 0;
    }
     
    Immortal Nephi, Apr 11, 2010
    #1
    1. Advertising

  2. Immortal Nephi

    Ian Collins Guest

    On 04/11/10 12:38 PM, Immortal Nephi wrote:
    > I sometimes put static keyword after class definition. Is static
    > safe to use with iostream classes? I need to place it in the function
    > body. The string in the function body stays in memory for lifetime
    > until program terminates.


    Your terminology is a bit of a muddle, but yes, you can have a static
    stream object in a function.

    --
    Ian Collins
     
    Ian Collins, Apr 11, 2010
    #2
    1. Advertising

  3. Immortal Nephi

    James Kanze Guest

    On Apr 11, 1:55 am, Ian Collins <> wrote:
    > On 04/11/10 12:38 PM, Immortal Nephi wrote:


    > > I sometimes put static keyword after class definition. Is
    > > static safe to use with iostream classes? I need to place
    > > it in the function body. The string in the function body
    > > stays in memory for lifetime until program terminates.


    > Your terminology is a bit of a muddle, but yes, you can have a
    > static stream object in a function.


    More than a bit. You can also use static after a class
    definition, e.g.:

    class Toto
    {
    // ...
    } static x;

    But you can't provide a class definition for std::eek:stream, for
    example, because it's already defined.

    Also, C has deprecated placing static in this place: if a
    storage class specifier is present, it should come first, e.g.:

    static std::eek:fstream log("toto.log");

    I would consider it good form in C++ to follow these rules as
    well.

    --
    James Kanze
     
    James Kanze, Apr 11, 2010
    #3
  4. On Apr 11, 7:33 am, James Kanze <> wrote:
    > On Apr 11, 1:55 am, Ian Collins <> wrote:
    >
    > > On 04/11/10 12:38 PM, Immortal Nephi wrote:
    > > > I sometimes put static keyword after class definition.  Is
    > > > static safe to use with iostream classes?  I need to place
    > > > it in the function body.  The string in the function body
    > > > stays in memory for lifetime until program terminates.

    > > Your terminology is a bit of a muddle, but yes, you can have a
    > > static stream object in a function.

    >
    > More than a bit.  You can also use static after a class
    > definition, e.g.:
    >
    >     class Toto
    >     {
    >         //  ...
    >     } static x;
    >
    > But you can't provide a class definition for std::eek:stream, for
    > example, because it's already defined.
    >
    > Also, C has deprecated placing static in this place: if a
    > storage class specifier is present, it should come first, e.g.:
    >
    >     static std::eek:fstream log("toto.log");
    >
    > I would consider it good form in C++ to follow these rules as
    > well.


    Trace function in my example is not a bit of muddle. I find some
    ways to add more flexibility. My code sounds like non-standard
    debugging report, but programmers easily understand my code when they
    examine class definition.
    James tells good example to add static to ofstream log. I think that
    he means to place it in the global scope or outside class definition
    in file scope.
    I want to add wrapper to class log. I place ofstream log in the
    class log body. I will use ofstream log when I want to write trace
    message to the file each line. Sometimes, I want to store trace
    message to static ostringstream log in the function body. If eExit
    ( enum variable ) is set to true condition, then all trace message
    lines stored in static ostringstream log will be displayed in the
    window MessageBox.
    The function looks like

    Trace( var < 10, “The value: var “ << var << “ must be less than “ <<
    var2 << “.\n” ).

    Notice operator << is placed in the function parameter. Sounds like
    invalid C++ rules? It is not a function, but it is a macro.

    #define Mtrace( expr, message ) \
    Trace( expr, message )

    I use prefix Hungarian notation and I can identify which is real
    function or macro. Very simple. A bit of muddle is rare.
     
    Immortal Nephi, Apr 11, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,526
    Tony Morris
    Oct 19, 2004
  2. Jianli Shen
    Replies:
    1
    Views:
    647
    Victor Bazarov
    Mar 13, 2005
  3. bugbear
    Replies:
    4
    Views:
    469
    bugbear
    Aug 23, 2006
  4. Robin
    Replies:
    0
    Views:
    439
    Robin
    Jun 6, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Decralation of class inside other class and definition outside this class

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 13, 2007, in forum: C++
    Replies:
    2
    Views:
    359
    Alf P. Steinbach
    Jul 13, 2007
Loading...

Share This Page