C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static member function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
MJ
Guest





PostPosted: Fri Apr 29, 2005 2:04 pm    Post subject: static member function Reply with quote



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

Back to top
Alvin
Guest





PostPosted: Fri Apr 29, 2005 2:13 pm    Post subject: Re: static member function Reply with quote



MJ wrote:

Quote:
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/beginnerctutorial/l/aa080802a.htm

--
Alvin

Back to top
Victor Bazarov
Guest





PostPosted: Fri Apr 29, 2005 2:14 pm    Post subject: Re: static member function Reply with quote



MJ wrote:
Quote:
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;

???

Quote:
static f();

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

???

Quote:
};

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

Yes.

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

Yes.

V

Back to top
abecedarian@spambob.com
Guest





PostPosted: Fri Apr 29, 2005 2:17 pm    Post subject: Re: static member function Reply with quote

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

// in *.cpp
int A::a = -1;

Quote:

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

float g = A::f();

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

int b = A::a;


Abe


Back to top
MJ
Guest





PostPosted: Fri Apr 29, 2005 2:53 pm    Post subject: Re: static member function Reply with quote

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

Back to top
Victor Bazarov
Guest





PostPosted: Fri Apr 29, 2005 2:58 pm    Post subject: Re: static member function Reply with quote

MJ wrote:
Quote:
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

Back to top
Alvin
Guest





PostPosted: Fri Apr 29, 2005 3:04 pm    Post subject: Re: static member function Reply with quote

MJ wrote:

Quote:
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

Back to top
MJ
Guest





PostPosted: Fri Apr 29, 2005 3:11 pm    Post subject: Re: static member function Reply with quote

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

Back to top
Rolf Magnus
Guest





PostPosted: Fri Apr 29, 2005 3:12 pm    Post subject: Re: static member function Reply with quote

MJ wrote:

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

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


Back to top
Victor Bazarov
Guest





PostPosted: Fri Apr 29, 2005 3:14 pm    Post subject: Re: static member function Reply with quote

Alvin wrote:
Quote:
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.

Quote:
[...]

Back to top
Victor Bazarov
Guest





PostPosted: Fri Apr 29, 2005 3:18 pm    Post subject: Re: static member function Reply with quote

MJ wrote:
Quote:
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.

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

Quote:
-------------------------------------------------------------
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?

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

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

Back to top
Rolf Magnus
Guest





PostPosted: Fri Apr 29, 2005 3:19 pm    Post subject: Re: static member function Reply with quote

MJ wrote:

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

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

Quote:
-------------------------------------------------------------
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


Back to top
MJ
Guest





PostPosted: Fri Apr 29, 2005 3:23 pm    Post subject: Re: static member function Reply with quote

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

Back to top
MJ
Guest





PostPosted: Fri Apr 29, 2005 3:29 pm    Post subject: Re: static member function Reply with quote

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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.