473,382 Members | 1,441 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,382 software developers and data experts.

Access static member variables

Hi Everyone,

I have the following code,

class Sample
{
public: static int i;
void f()
{
Sample::i = 5;
}
};

int main()
{
Sample obj;
obj.f(); // Causes linker error saying unresolved
external symbol Sample::i...
}

It works fine, if the following statement is added before main and
after the class declaration,

int Sample::i;

Does the above statement take care of memory allocation for the
static member variable?
Dec 5 '07 #1
2 2901
Rahul wrote:
Hi Everyone,

I have the following code,

class Sample
{
public: static int i;
void f()
{
Sample::i = 5;
There is no need to qualify the name 'i' here, BTW.
}
};

int main()
{
Sample obj;
obj.f(); // Causes linker error saying unresolved
external symbol Sample::i...
}

It works fine, if the following statement is added before main and
after the class declaration,

int Sample::i;

Does the above statement take care of memory allocation for the
static member variable?
Yes.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 5 '07 #2
Rahul wrote:
Hi Everyone,

I have the following code,

class Sample
{
public: static int i;
void f()
{
Sample::i = 5;
}
};

int main()
{
Sample obj;
obj.f(); // Causes linker error saying unresolved
external symbol Sample::i...
}

It works fine, if the following statement is added before main and
after the class declaration,

int Sample::i;

Does the above statement take care of memory allocation for the
static member variable?
Yes. See FAQ 10.11 http://parashift.com/c++-faq-lite/ctors.html#faq-10.11
Dec 5 '07 #3

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

Similar topics

7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
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...
3
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
3
by: tom | last post by:
How to get access to veriable from static class member? class cTest { public: int a; void Set_a(){a = 1;} static int dlgTest()
2
by: Ahmed | last post by:
Ok i understand the fact that once events are raised they are executed on another thread and have no access to member functions or variables because they are static functions. But i am having...
6
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...

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.