Regarding Static

Discussion in 'C Programming' started by sonu, Mar 8, 2006.

  1. sonu

    sonu Guest

    Hi All,

    Pls clarify me what is the difference between static member and static
    method in c.


    pls some one reply me with Example.
     
    sonu, Mar 8, 2006
    #1
    1. Advertisements

  2. There is no difference, since neither exists. Perhaps you were thinking
    of C++ or Java?

    -- Richard
     
    Richard Tobin, Mar 8, 2006
    #2
    1. Advertisements

  3. sonu

    sonu Guest

    Hi Richard, is this any different in c ONLY
     
    sonu, Mar 8, 2006
    #3
  4. Is *what* any different in "c ONLY"???

    Quote what and who you're replying to. See
    <http://cfaj.freeshell.org/google/>

    Looking at your original post, and Richard's reply, I can't quite see
    why you didn't understand the answer (or you didn't understand the
    question?). To try to re-phrase it for you:

    The things you mention (static members and static methods) do not exist
    in C programming language (as standardised by ISO/ANSI). They do,
    however, exist in C++ (an entirely different kettle of fish) and Java
    (more obviously so). These languages are off-topic in this newsgroup,
    and you should re-direct your question to comp.lang.c++ or
    comp.lang.java.
     
    Vladimir S. Oka, Mar 8, 2006
    #4
  5. As I said, static members and methods don't exist in C. They are
    things that exist in some object oriented languages such as Java and
    C++.

    -- Richard
     
    Richard Tobin, Mar 8, 2006
    #5
  6. sonu

    John Bode Guest

    You're thinking of C++, not C. Followups set to comp.lang.c++.

    My C++ is still a bit weak, so don't consider this to be authoritative.
    Static members and static methods do not belong to a particular class
    instance, and are not referenced through a particular class instance.
    Instead, you refer to them using the class name and scope resolution
    operator ::. The best way to illustrate it is with an example:

    #include <iostream>

    using std::cout;
    using std::endl;

    class MyClass
    {
    public:

    MyClass()
    {
    m_staticInstance++;
    }

    ~MyClass()
    {
    m_staticInstance--;
    }

    // Instance method
    int getInstanceValue()
    {
    return m_instance;
    }

    // Static method
    static void setStaticInstance(int value)
    {
    m_staticInstance = value;
    }

    // Instance member
    int m_instance;

    // Static member
    static int m_staticInstance;
    };

    // If you declare a static member inside of a
    // class, you must provide a corresponding
    // definition of it outside of the class;
    // this is necessary since the member
    // has to exist independently
    // of any class instance.

    int MyClass::m_staticInstance = 0;

    int main(void)
    {
    MyClass instance0, instance1;

    // Refer to instance members and methods through
    // the instance.

    instance0.m_instance = 100;
    instance1.m_instance = 200;

    cout << "instance0.m_instance: "
    << instance0.m_instance << endl
    << "instance1.m_instance: "
    << instance1.m_instance << endl
    << "instance0.getInstanceValue(): "
    << instance0.getInstanceValue() << endl
    << "instance1.getInstanceValue(): "
    << instance1.getInstanceValue() << endl;


    // Refer to static members and methods through
    // the class name.
    cout << "MyClass::m_staticInstance: "
    << MyClass::m_staticInstance << endl
    << "MyClass::getStaticInstance(): "
    << MyClass::m_staticInstance << endl;

    MyClass::m_staticInstance = 3;

    // Refer to static members and methods through
    // the class name.
    cout << "MyClass::m_staticInstance: "
    << MyClass::m_staticInstance << endl
    << "MyClass::getStaticInstance(): "
    << MyClass::m_staticInstance << endl;

    return 0;
    }

    And the output:

    john@marvin ~/prototypes/C++/static_demo $ ./static_demo
    instance0.m_instance: 100
    instance1.m_instance: 200
    instance0.getInstanceValue(): 100
    instance1.getInstanceValue(): 200
    MyClass::m_staticInstance: 2
    MyClass::getStaticInstance(): 2
    MyClass::m_staticInstance: 3
    MyClass::getStaticInstance(): 3
     
    John Bode, Mar 8, 2006
    #6
  7. sonu

    Default User Guest


    For future occasions, be sure to crosspost as well as set follow-ups in
    these situations. As it is, you posted a bunch of C++ that won't be
    seen in clc++ unless someone replies, in which case they'll have no
    idea what the original message was.




    Brian
     
    Default User, Mar 8, 2006
    #7
  8. sonu

    Jaspreet Guest

    I still seem to be missing something or probably did not sleep well
    yesterday. Isn't the OP talking of static variables in C ? Or is it
    just me who is halucinating ?

    Static variables do exist in C. If you have a static variable inside a
    function the lifetime of the variable is till the life of the program.

    I know you guys (and gals) know that but am I still missing something ?
     
    Jaspreet, Mar 9, 2006
    #8
  9. The OP asked about "static members" (in C, only structs have members,
    and they can't be static) and "static methods" (C doesn't have
    anything called "methods"; object-oriented languages often do).

    If he wants to ask about static variables, he can certainly come back
    here and do so (or read his C textbook).
     
    Keith Thompson, Mar 9, 2006
    #9
  10. sonu

    Jaspreet Guest

    Thanks Keith for letting me know I am in my senses today. :) Yeah, let
    the OP come back with his version of the question rather than we making
    any assumptions on the exact query he wanted to ask.
     
    Jaspreet, Mar 9, 2006
    #10
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.