LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-26-2002, 09:56 AM   #1
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Rep: Reputation: 0
Question static functions in C++


i have written a code like this in C++ on linux version 6.2

class sample
{
static int a;
public:
static void accept()
{
cout<<"Enter a value for a"<<endl;
cin>>a;
}
static void disp()
{
cout<<a<<endl;
}
};
void main()
{
sample::accept();
sample::disp();
}
is this code valid?static functions are created before any objects are created right?so they can be refered by the class name and they can be called by scope resolution operator.
but this code is giving me an error.can anyone solve this problem.or may be there is some error in program?

bye
abida
 
Old 07-26-2002, 10:51 AM   #2
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
add this after the declaration of the class:

int sample::a = 0;
 
Old 07-27-2002, 09:34 AM   #3
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Original Poster
Rep: Reputation: 0
hi champ
u didn't explain y this kind of initialisation has to be given?
i think on unix platform the same program works even without this kind of initialisation,then y not on linux?
 
Old 07-27-2002, 11:04 AM   #4
champ
Member
 
Registered: Jul 2002
Distribution: Slackware 10.0
Posts: 46

Rep: Reputation: 16
The declaration of a does not define an integer; no storage space is set aside.
Unlike the non-static member variables, no storage space is set aside by instantiating a sample object, because the a member variable is not in the object. Thus, variable a has to be defined and initialized.
 
Old 07-31-2002, 12:05 PM   #5
concoran
Member
 
Registered: Jun 2001
Location: 28N,82W
Distribution: XP,Ubuntu 9
Posts: 468

Rep: Reputation: 30
1. The C++ is platform independent. The language as such
doesn't care if it's Unix or Mac.
2. int sample::a = 0; is important from Linker's perspective.
What that means is : variable a is static and belongs to class
sample which in turn means, the variable a exists even if you
do not create any object of 'sample'. In short, it's a global
variable, but with a little more security. Since it's a global var,
it needs to be defined (not just declared).
3. BTW, in your code, 'disp' need not be a 'static' method. IT can
as well be a regular one.
 
Old 08-02-2002, 09:25 AM   #6
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Original Poster
Rep: Reputation: 0
hey thanx a lot for ur replies ,now i got the solution for my problem,thanx one and all
bye
abida
 
Old 08-14-2002, 02:51 AM   #7
guest72485
LQ Newbie
 
Registered: Jun 2002
Posts: 8

Rep: Reputation: 0
Quote:
Originally posted by concoran
3. BTW, in your code, 'disp' need not be a 'static' method. IT can as well be a regular one.
why is that so, i thought static membe variables can be accessed only by static member functions. am i wrong?
 
Old 07-13-2005, 04:09 PM   #8
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
Quote:
Originally posted by guest72485
why is that so, i thought static membe variables can be accessed only by static member functions. am i wrong?
It's the other way around.

A static member has a lifetime of the entire run-time of the program.

A non-static (auto, register or volatile) member has the same lifetime as the object to which it belongs (from the start of the constructor body after object allocation to the end of the destructor body before object clean-up). If you have multiple objects of the same class, each object gets its own member variable, seperate and independant of the others.

A static method also has a “lifetime” of the entire run-time of the program; it's just a normal method that happens to be in the scope of a class, and subject to class protection rules.

A non-static method is implicitly passed a reference to the object by a hidden parameter (called this). So you need to have a defined object in order for the method to be called; if no object is defined, then the method can't be called. (So, while you could make disp non-static, you would need to allocate an object of type sample in order to call it).

Any method can access a static member variable, assuming that it's got sufficient permissions to do so (i.e. is in the same class; all member variables should always be declared private).

A non-static member variable can only be accessed from the object to which it belongs. Since a static member does not belong to an object, it can't access non-static variables.


Btw, the current version of Linux is 2.6.something (last time I checked), so I suspect that 6.2 is the version of your distribution. Which doesn't tell us anything because you've not said which distribution you're using.

Hope that helps,

— Robert J. Lee
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM
IPC - semphores and static functions Hko Programming 1 01-12-2004 04:55 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM


All times are GMT -5. The time now is 05:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration