 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Surya Kiran Guest
|
Posted: Wed Jan 28, 2004 6:57 am Post subject: 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
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Wed Jan 28, 2004 9:03 am Post subject: Re: Static data memer |
|
|
"Surya Kiran" <skg (AT) fluent (DOT) co.in> wrote
| Quote: | 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 ?
|
|
| Back to top |
|
 |
lilburne Guest
|
Posted: Wed Jan 28, 2004 9:38 am Post subject: Re: Static data memer |
|
|
Surya Kiran wrote:
| Quote: |
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.
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Wed Jan 28, 2004 12:12 pm Post subject: Re: Static data memer |
|
|
"Surya Kiran" <skg (AT) fluent (DOT) co.in> skrev i en meddelelse
news:5946f326.0401272257.49312d40 (AT) posting (DOT) google.com...
| Quote: | 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.
| Quote: |
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
|
|
| Back to top |
|
 |
Surya Kiran Guest
|
Posted: Thu Feb 05, 2004 1:51 pm Post subject: Re: Static data memer |
|
|
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" <pklspam (AT) mailme (DOT) dk> wrote
| Quote: | "Surya Kiran" <skg (AT) fluent (DOT) co.in> skrev i en meddelelse
news:5946f326.0401272257.49312d40 (AT) posting (DOT) google.com...
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
};
// source
std::list<B> A::list_of_b;
Kind regards
Peter
|
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Feb 05, 2004 2:57 pm Post subject: Re: Static data memer |
|
|
"Surya Kiran" <skg (AT) fluent (DOT) co.in> wrote...
| Quote: | 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?
| Quote: |
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.
| Quote: | 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.
| Quote: |
Please clarify.
Thanks in advance,
Surya
"Peter Koch Larsen" <pklspam (AT) mailme (DOT) dk> wrote
"Surya Kiran" <skg (AT) fluent (DOT) co.in> skrev i en meddelelse
news:5946f326.0401272257.49312d40 (AT) posting (DOT) google.com...
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
};
// source
std::list<B> A::list_of_b;
Kind regards
Peter
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|