473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

static member function

MJ
Hi
I have a basic doubt about structure.
I have a class in which a I have declared a static funtion
class A
{
public:
static a;
static f();
};

Can I access the the static function without creating a instance of a
class A.
Can I access the static variable without creating a instance of a class
A.

MJ

Jul 23 '05 #1
13 3211
MJ wrote:
Hi
I have a basic doubt about structure.
I have a class in which a I have declared a static funtion
class A
{
public:
static a;
static f();
};

Can I access the the static function without creating a instance of a
class A.
Can I access the static variable without creating a instance of a class
A.

MJ


http://cplus.about.com/od/beginnerct.../aa080802a.htm

--
Alvin
Jul 23 '05 #2
MJ wrote:
I have a basic doubt about structure.
I have a class in which a I have declared a static funtion
class A
{
public:
static a;
static <what type?> a;

???
static f();
static <what return value type?> f();

???
};

Can I access the the static function without creating a instance of a
class A.
Yes.
Can I access the static variable without creating a instance of a class
A.


Yes.

V
Jul 23 '05 #3
MJ wrote:
Hi
I have a basic doubt about structure.
I have a class in which a I have declared a static funtion
class A
{
public: static int a;
static float f() { return 3.14; } };
// in *.cpp
int A::a = -1;

Can I access the the static function without creating a instance of a
class A.
float g = A::f();
Can I access the static variable without creating a instance of a class A.


int b = A::a;
Abe

Jul 23 '05 #4
MJ
Hi
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};

int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}
--------------------------------------------------------------

if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
but the static function I am able to access properly and there is no
problem
if I put both the lines out side the main loop it does not give me any
error
So does it mean that the static function are accessible but static
variable are specific to scope ??
MJ

Jul 23 '05 #5
MJ wrote:
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};

int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}
--------------------------------------------------------------

if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
but the static function I am able to access properly and there is no
problem
if I put both the lines out side the main loop it does not give me any
error
So does it mean that the static function are accessible but static
variable are specific to scope ??


No. It means you're not defining the static data member where _required_.
What does your C++ book say about defining static data members? What C++
book are you reading?

V
Jul 23 '05 #6
MJ wrote:
Hi
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};

int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}
--------------------------------------------------------------

if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
but the static function I am able to access properly and there is no
problem
if I put both the lines out side the main loop it does not give me any
error
So does it mean that the static function are accessible but static
variable are specific to scope ??
MJ


Just remove the *int* from the line 1. In main() all you want to do is alter
the static variable not re-declare it. So it would be:

int main()
{
A::a = -1;
...
}

Also, there is another bug. A::a is undefined. You say what the *initial
value* of A::a because check it as in:

int main()
{
if(A::a > 10)
cout << "A::a is greater than 10" << endl;
...
}

With your current implementation, the above code will have undefined
behaviour (that just means it will act weird).

--
Alvin
Jul 23 '05 #7
MJ wrote:
Hi
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};

int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}
--------------------------------------------------------------

if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
And that's right. line 1 tries to define A::a, which isn't allowed within a
function. You have to define A::a, but outside of a function.
but the static function I am able to access properly and there is no
problem if I put both the lines out side the main loop it does not give
me any error.
So does it mean that the static function are accessible but static
variable are specific to scope ??


Nope. It means your syntax is wrong. Try:

#include <iostream>

class A
{
public:
static int a;
static float f() { return 3.14;}
};

int A::a;

int main () {
using namespace std;
A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}

Jul 23 '05 #8
MJ
Hi
I am reading scott mayor and thinking in C++ vol i and II
Sorry I could not get what you mean ... I have declared the static data
member in the class itself.. and I can do like that .. whats wrong in
that ....

See the code below it works fine when I am accessing the static
variable outside
-------------------------------------------------------------
class A
{
private:

public:
static int a;
static float f() { return 3.14;}
};
int A::a = -1;
int main () {
// A::a = -1;
float g = A::f();
cout << g << endl;
cin >> g;

return 0 ;
}
-------------------------------------------------------------
MJ

Jul 23 '05 #9
Alvin wrote:
MJ wrote:

Hi
I tried the following code
--------------------------------------------------------------
class A
{
public:
static int a;
static float f() { return 3.14;}
};

int main () {
//int A::a = -1; // line 1
float g = A::f(); // line 2
cout << g << endl;
cin >> g;

return 0 ;
}
--------------------------------------------------------------

if i uncommet the line 1 its giving me an error " definition or
redeclaration illegal in current scope"
but the static function I am able to access properly and there is no
problem
if I put both the lines out side the main loop it does not give me any
error
So does it mean that the static function are accessible but static
variable are specific to scope ??
MJ

Just remove the *int* from the line 1. In main() all you want to do is alter
the static variable not re-declare it. So it would be:

int main()
{
A::a = -1;
...
}

Also, there is another bug. A::a is undefined.


It's not a bug. The code won't link if A::a is undefined.
[...]

Jul 23 '05 #10
MJ wrote:
Hi
I am reading scott mayor and thinking in C++ vol i and II
Sorry I could not get what you mean ... I have declared the static data
member in the class itself..
Yes. You have _declared_, but not _defined_ it there.
and I can do like that .. whats wrong in that ....

See the code below it works fine when I am accessing the static
variable outside
You're not _accessing_ it, you're _defining_ it. Now that it's defined, it
should be accessible from within main.
-------------------------------------------------------------
class A
{
private:

public:
static int a;
static float f() { return 3.14;}
};
int A::a = -1;
int main () {
// A::a = -1;
float g = A::f();
cout << g << endl;
cin >> g;

return 0 ;
}
-------------------------------------------------------------
MJ


Jul 23 '05 #11
MJ wrote:
I am reading scott mayor and thinking in C++ vol i and II
Sorry I could not get what you mean ... I have declared the static data
member in the class itself.. and I can do like that .. whats wrong in
that ....
Absolutely nothing. You did the right thing. And you actually used the
right term describing what you did: *declared* the static data.
See the code below it works fine when I am accessing the static
variable outside
You're not accessing it. You're _defining_ it "outside". Using it
"inside" should be fine. Uncomment the statement where you're using it
and you will see that it's still OK.
-------------------------------------------------------------
class A
{
private:

public:
static int a;
static float f() { return 3.14;}
};
int A::a = -1;
Now it's fine. Here you defined it. It wasn't here before, was it?
int main () {
// A::a = -1;
float g = A::f();
cout << g << endl;
cin >> g;

return 0 ;
}
-------------------------------------------------------------

Jul 23 '05 #12
MJ
Hi
I had tried that ... removing the *int*
but the code does not work... regarding the initialization ... the
static variables get initialized to zero...
see the code below
-----------------------------------------------
static k;
int main () {
cout << k << endl;
return 0 ;
}
----------------------------------------------------

this will print the value of k = 0.... so the static k will get
initialized at the time of compilation....
But I am not sure what happens to the static variable in a class ...
MJ

Jul 23 '05 #13
MJ
Hi
Yes I got it
It was a mistake I did .... the static variable should be defined
outside the scope of the class ...
in case of global static variable they get defined and declared ....
I tried and its working fine
the code is as
--------------------------------------
class A
{
private:

public:
static int a;
static float f()
{
A::a = 10;
//cout << a << endl;
return 3.14;
}
};
int A::a;// = -1;
int main () {
A::a = -10;
float g = A::f();
cout << A::a << endl;
cin >> g;

return 0 ;
}
------------------------------------------------

This code is working fine ....
Thanks a lot
MJ .....

Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Naren | last post by:
Hello Grp, Correct me if I am wrong. static member functions can act only on static member varaibles.It can accessed by using the name of the class. Then why is there an access controller. ...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
4
by: raghavendra | last post by:
Hi , A static member can be accessed only by another static method....but the vice-versa is not true....Can anyone pls explain me the logic behind this... Also in a project, if we have too many...
8
by: SJ | last post by:
Hi: I have a class which has a static member function. The function implements something common to all instances. How can the static member function know all of the (Get access to the instances'...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
5
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
7
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
5
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.