static member function

M

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
 
V

Victor Bazarov

MJ said:
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
 
A

abecedarian

MJ said:
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
 
M

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
 
V

Victor Bazarov

MJ said:
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
 
A

Alvin

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

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 ;
}
 
R

Rolf Magnus

MJ said:
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 ;
}

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 ;
}
 
V

Victor Bazarov

Alvin said:
MJ wrote:




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

Victor Bazarov

MJ said:
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?
 
R

Rolf Magnus

MJ said:
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.
 
M

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
 
M

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 ;
}
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top