473,406 Members | 2,620 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,406 software developers and data experts.

Static data memer

Hi all,
I've 2 classes class A, and class B.
and i want to use list of type B in class A. like this

list<B> typeB ;

till now its fine. But i want to make it a static member. like

static list<B> typeB ;

declaration is not a problem. Definition is the problem. I've 2
constructors for class B
1. default constructor B()
2. B (int a, int b)

how can i define the static member in class A, for both the
constructors.

Thanks in advance,
Surya
Jul 22 '05 #1
5 1535

"Surya Kiran" <sk*@fluent.co.in> wrote in message
news:59**************************@posting.google.c om...
Hi all,
I've 2 classes class A, and class B.
and i want to use list of type B in class A. like this

list<B> typeB ;

till now its fine. But i want to make it a static member. like

static list<B> typeB ;

declaration is not a problem. Definition is the problem. I've 2
constructors for class B
1. default constructor B()
2. B (int a, int b)

how can i define the static member in class A, for both the
constructors.


How about posting the code and the error you get ?
Jul 22 '05 #2


Surya Kiran wrote:

how can i define the static member in class A, for both the
constructors.


Static members are only constructed once, so as you don't get to
construct it multiple times, therefor choose the B ctor that is most
relevant. Most probably the default one.

Jul 22 '05 #3

"Surya Kiran" <sk*@fluent.co.in> skrev i en meddelelse
news:59**************************@posting.google.c om...
Hi all,
I've 2 classes class A, and class B.
and i want to use list of type B in class A. like this

list<B> typeB ;
This name is confusing - it is not a type.
till now its fine. But i want to make it a static member. like

static list<B> typeB ;

declaration is not a problem. Definition is the problem. I've 2
constructors for class B
1. default constructor B()
2. B (int a, int b)

how can i define the static member in class A, for both the
constructors.

Thanks in advance,
Surya


Well... a static member - if this is what you want - is not declared in a
constructor. This is what you want:

// header
#include <list>
class B {};

class A
{
static std::list<B> list_of_b;
};

// source
std::list<B> A::list_of_b;

Kind regards
Peter
Jul 22 '05 #4
My question is how do we initialize a pointer to static member.

if its a ordinary class (class MyClass) we can initialize it to NULL,
but my question is

//someclass.hpp
class someclass
{
....
static list<MyClass> *mylist ; //declaring is over.
....
};

//Lets assume i'm initialize it in someclass.cpp
//how do i initialize it.
//obviously not like this
// list<MyClass>* someclass::mylist = 0;

Please clarify.
Thanks in advance,
Surya

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message news:<RG*********************@news000.worldonline. dk>...
"Surya Kiran" <sk*@fluent.co.in> skrev i en meddelelse
news:59**************************@posting.google.c om...
Hi all,
I've 2 classes class A, and class B.
and i want to use list of type B in class A. like this

list<B> typeB ;


This name is confusing - it is not a type.

till now its fine. But i want to make it a static member. like

static list<B> typeB ;

declaration is not a problem. Definition is the problem. I've 2
constructors for class B
1. default constructor B()
2. B (int a, int b)

how can i define the static member in class A, for both the
constructors.

Thanks in advance,
Surya


Well... a static member - if this is what you want - is not declared in a
constructor. This is what you want:

// header
#include <list>
class B {};

class A
{
static std::list<B> list_of_b;
};

// source
std::list<B> A::list_of_b;

Kind regards
Peter

Jul 22 '05 #5
"Surya Kiran" <sk*@fluent.co.in> wrote...
My question is how do we initialize a pointer to static member.
And your example shows a static member that is a pointer, not a pointer
to static member. Do you actually know what you want?

if its a ordinary class (class MyClass) we can initialize it to NULL,
You can initialise any pointer to 0, regardless what it points to.
but my question is

//someclass.hpp
class someclass
{
...
static list<MyClass> *mylist ; //declaring is over.
...
};

//Lets assume i'm initialize it in someclass.cpp
//how do i initialize it.
//obviously not like this
// list<MyClass>* someclass::mylist = 0;
Yes, that's the way, if you want to be explicit. Static data are
initialised to 0 anyway, even if you don't provide an initialiser.

Please clarify.
Thanks in advance,
Surya

"Peter Koch Larsen" <pk*****@mailme.dk> wrote in message

news:<RG*********************@news000.worldonline. dk>...
"Surya Kiran" <sk*@fluent.co.in> skrev i en meddelelse
news:59**************************@posting.google.c om...
Hi all,
I've 2 classes class A, and class B.
and i want to use list of type B in class A. like this

list<B> typeB ;


This name is confusing - it is not a type.

till now its fine. But i want to make it a static member. like

static list<B> typeB ;

declaration is not a problem. Definition is the problem. I've 2
constructors for class B
1. default constructor B()
2. B (int a, int b)

how can i define the static member in class A, for both the
constructors.

Thanks in advance,
Surya


Well... a static member - if this is what you want - is not declared in a constructor. This is what you want:

// header
#include <list>
class B {};

class A
{
static std::list<B> list_of_b;
};

// source
std::list<B> A::list_of_b;

Kind regards
Peter

Jul 22 '05 #6

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

Similar topics

29
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...
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...
8
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'...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
18
by: Erik Arner | last post by:
Hi, I really need some help here. After upgrading to g++ 3.4 I have run into all sorts of troubles that I'm sure depends on my lack of proper understanding of C++. I would now like to get it right...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
7
by: Jon Vaughan | last post by:
I have a piece of code that I want to run on a Pocket Pc, I have written a data class that will store the small amount of data that is required for the program. As this class will be used via a few...
4
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.