473,699 Members | 2,738 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1549

"Surya Kiran" <sk*@fluent.co. in> wrote in message
news:59******** *************** ***@posting.goo gle.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 ;

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.goo gle.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<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::myli st = 0;

Please clarify.
Thanks in advance,
Surya

"Peter Koch Larsen" <pk*****@mailme .dk> wrote in message news:<RG******* **************@ news000.worldon line.dk>...
"Surya Kiran" <sk*@fluent.co. in> skrev i en meddelelse
news:59******** *************** ***@posting.goo gle.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<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::myli st = 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.worldon line.dk>...
"Surya Kiran" <sk*@fluent.co. in> skrev i en meddelelse
news:59******** *************** ***@posting.goo gle.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<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
4394
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
11
4604
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
8
1828
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' handles) instances? Thanks in advance for any help
8
4582
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
18
5754
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 once and for all, if possible. Most severe is the problem illustrated by the code below. It's based on the "pluggable factory pattern" described in http://www.adtmag.com/joop/crarticle.asp?ID=1520
3
2088
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 copy of data as opposed to multiple instances of the class) C# is now applying the static concept to methods. Why?, I thought that only one copy of the methods were loaded into memory not matter how many instances were created. Is this different...
12
2569
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 advance, Joe
7
2396
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 forms in the program, what the best way to setup this class - Static ? and if so where do I enter the setup for the initial fetch, as at the moment its in the constructor, i'm right in thinking there is no constructor in a static class. Thanks
4
4527
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 child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
9171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.