473,890 Members | 1,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3285
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
redeclarati on 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

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

Similar topics

5
9392
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. what does a private static member function mean? Thaanx in advance for any advice Rgds,
29
4422
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
11
4637
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
4
1962
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 static members and methods, will it cause a problem?? thanx for ur comments regards, Raghavendra Mahuli
8
1848
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' handles) instances? Thanks in advance for any help
15
6605
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 either has access only to static members of the class (ie. assuming no object of the class is in scope - neither by arguments recieved nor by local declarations). Any static member function like this: //accessing static member i static void...
5
651
by: Tony Johansson | last post by:
Hello experts! Why is not possible to have virtual static members Many thnakn //Tony
7
3048
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 is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
11
3859
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
5
2512
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 different versions of callbacks (see the code below for 3 variants). Variant1: a normal static member function is used to redirect the callback to a member function Variant2: a static member function is declared, but at the definition the
0
9976
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10802
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10451
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9618
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8008
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4665
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4259
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.