473,769 Members | 4,591 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 3271
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
9387
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
4413
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
4616
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
1960
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
1837
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
6593
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
3039
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
3843
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
2506
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
9590
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
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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
10051
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...
0
9866
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
8879
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
7413
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...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3571
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.