Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Static data memer

Reply
Thread Tools

Static data memer

 
 
Surya Kiran
Guest
Posts: n/a
 
      01-28-2004
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
 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      01-28-2004

"Surya Kiran" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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 ?


 
Reply With Quote
 
 
 
 
lilburne
Guest
Posts: n/a
 
      01-28-2004


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.

 
Reply With Quote
 
Peter Koch Larsen
Guest
Posts: n/a
 
      01-28-2004

"Surya Kiran" <(E-Mail Removed)> skrev i en meddelelse
news:(E-Mail Removed) 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


 
Reply With Quote
 
Surya Kiran
Guest
Posts: n/a
 
      02-05-2004
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" <(E-Mail Removed)> wrote in message news:<RGNRb.80000$(E-Mail Removed). dk>...
> "Surya Kiran" <(E-Mail Removed)> skrev i en meddelelse
> news:(E-Mail Removed) 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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-05-2004
"Surya Kiran" <(E-Mail Removed)> 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" <(E-Mail Removed)> wrote in message

news:<RGNRb.80000$(E-Mail Removed). dk>...
> > "Surya Kiran" <(E-Mail Removed)> skrev i en meddelelse
> > news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
local static data vs. global data Leslaw Bieniasz C++ 3 10-24-2012 08:42 AM
Why inner class can not have static data, static fields or nested class? Robin Java 0 06-06-2007 11:16 AM
PIX public/24 ip static mapping means 256 times interfaces static maps? Nieuws Xs4all Cisco 0 05-26-2005 11:07 AM
Assign class memer in body of constructor Tony Johansson C++ 3 05-20-2005 04:48 PM
CRT, Member Initialisation, static data, constant data, global objects Tim C++ 2 12-15-2003 07:15 PM



Advertisments