Static data memer

S

Surya Kiran

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
 
S

Sharad Kala

Surya Kiran said:
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 ?
 
L

lilburne

Surya said:
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.
 
P

Peter Koch Larsen

Surya Kiran said:
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
 
S

Surya Kiran

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
 
V

Victor Bazarov

Surya Kiran said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top