Regarding Static


S

sonu

Hi All,

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


pls some one reply me with Example.
 
Ad

Advertisements

R

Richard Tobin

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

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

-- Richard
 
V

Vladimir S. Oka

sonu said:
Hi Richard, is this any different in c ONLY

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.
 
R

Richard Tobin

Hi Richard, is this any different in c ONLY

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
 
J

John Bode

sonu said:
Hi All,

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


pls some one reply me with Example.

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:

[email protected] ~/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
 
Ad

Advertisements

D

Default User

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


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
 
J

Jaspreet

Default said:
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.
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 ?
 
K

Keith Thompson

Jaspreet said:
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 ?

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).
 
Ad

Advertisements

J

Jaspreet

Keith said:
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).

--
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.
 

Top