473,881 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

When is a static data member defined?

In the following code, at what point is S::c fully defined?

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class C {
int _v;
public:
C(const int& v)
:_v(v)
{}

ostream& print(ostream& out) const
{
return out << _v;
}
};

struct S {
static const C c;
};

const C S::c = C(42);

ostream& operator<<(ostr eam& out, const S& s)
{
s.c.print(out);
return out;
}
int main()
{
S s;
cout << s << endl;
return 0;
}

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #1
24 2779
Steven T. Hatton wrote:
In the following code, at what point is S::c fully defined?

#include <iostream>
[...]
const C S::c = C(42);
Here.
[...]
int main()
{
S s;
cout << s << endl;
return 0;
}


V
Jul 22 '05 #2
Steven T. Hatton wrote:
In the following code, at what point is S::c fully defined?

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class C {
int _v;
public:
C(const int& v)
:_v(v)
{}

ostream& print(ostream& out) const
{
return out << _v;
}
};

struct S {
static const C c;
};

const C S::c = C(42);

Here.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3
Victor Bazarov wrote:
Steven T. Hatton wrote:
In the following code, at what point is S::c fully defined?

#include <iostream>
[...]
const C S::c = C(42);


Here.


I agree. At what point is the memory (typically) allocated?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #4
Steven T. Hatton wrote:

I agree. At what point is the memory (typically) allocated?


S::c is statically allocated. You don't know when it is allocated
other than it's there before it can be used. Typically, it's just
created when the program is loaded (i.e, before execution actually
starts).
Jul 22 '05 #5
Ron Natalie wrote:
Steven T. Hatton wrote:

I agree. At what point is the memory (typically) allocated?


S::c is statically allocated. You don't know when it is allocated
other than it's there before it can be used. Typically, it's just
created when the program is loaded (i.e, before execution actually
starts).

I was speaking in terms used when discussing issues related to the use of
data members verses pointers or references to data members. For example,
if we have an indirect recursive structure such as:

#include <iostream>
class B;

class A{
B b;
};

class B{
A a;
};

It is commonly asserted that the above code will not compile because 'the
compiler doesn't know how to allocate the memory for b'. I guess that
means the compiler doesn't have sufficient information to determine the
relative offset for the end of the storage location intended to hold the
instance B A::b; So my question might be rephrased: when is the memory
allocation determined for the static data member?
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #6
Steven T. Hatton wrote:

It is commonly asserted that the above code will not compile because 'the
compiler doesn't know how to allocate the memory for b'.
It knows how, it just doesn't know how much :-). Non-static members
must be of complete types, 9.2/8.
So my question might be rephrased: when is the memory
allocation determined for the static data member?


At the point of definition. It's declaration in the class is
allowed to be an incomplete type. See 9.4.2/2. The definition
(where you have const C S::c = C(42);) does require the complete
type.
Jul 22 '05 #7
Steven T. Hatton wrote:
Ron Natalie wrote:

Steven T. Hatton wrote:

I agree. At what point is the memory (typically) allocated?
S::c is statically allocated. You don't know when it is allocated
other than it's there before it can be used. Typically, it's just
created when the program is loaded (i.e, before execution actually
starts).


I was speaking in terms used when discussing issues related to the use of
data members verses pointers or references to data members. For example,
if we have an indirect recursive structure such as:

#include <iostream>
class B;

class A{
B b;
};

class B{
A a;
};

It is commonly asserted that the above code will not compile because 'the
compiler doesn't know how to allocate the memory for b'. I guess that
means the compiler doesn't have sufficient information to determine the
relative offset for the end of the storage location intended to hold the
instance B A::b;


Right. The _size_ of the object is the problem here.
So my question might be rephrased: when is the memory
allocation determined for the static data member?


Memory allocation for any object is determined when the object has
a complete type.

V
Jul 22 '05 #8
Victor Bazarov wrote:
Steven T. Hatton wrote:
> So my question might be rephrased: when is the memory
allocation determined for the static data member?


Memory allocation for any object is determined when the object has
a complete type.


So, in the example I posted, that would be when the member declaration S::C
s is encountered in the struct definition.

Here's just something to think about:

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class C {
int _v;
public:

C(const int& v = 0)
:_v(v)
{}

ostream& print(ostream& out) const
{
return out << _c._v << " " << _v;
}

static const C _c;
};
struct S {
static const C c;
};

const C S::c = C(42);

ostream& operator<<(ostr eam& out, const S& s)
{
s.c.print(out);
return out;
}
const C C::_c = C(1066);

int main()
{
S s;
cout << s << endl;
return 0;
}

--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell

Jul 22 '05 #9
Steven T. Hatton wrote:
Victor Bazarov wrote:

Steven T. Hatton wrote:
> So my question might be rephrased: when is the memory
allocation determined for the static data member?


Memory allocation for any object is determined when the object has
a complete type.

So, in the example I posted, that would be when the member declaration S::C
s is encountered in the struct definition.

Here's just something to think about:


So, have _you_ thought about it?

#include <iostream>

using std::cout;
using std::endl;
using std::ostream;

class C {
int _v;
public:

C(const int& v = 0)
:_v(v)
{}

ostream& print(ostream& out) const
{
return out << _c._v << " " << _v;
}

static const C _c;
That's a declaration.
};
struct S {
static const C c;
That's another declaration.
};

const C S::c = C(42);
That's a definition. No problem. 'C' is a complete type.

ostream& operator<<(ostr eam& out, const S& s)
{
s.c.print(out);
return out;
}
const C C::_c = C(1066);
That's another definition. Again, no problem.

int main()
{
S s;
cout << s << endl;
return 0;
}


So I've thought about it. Now what?

V
Jul 22 '05 #10

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

Similar topics

3
343
by: marcus | last post by:
I have a class which has one static data member. The .h file containing this class is included from many cpp files. Therefor I have the definition part of the data member in the implementation of the class (in the .cpp file) to avoid getting "multiple defined..." error. In the .cpp file I put the definition outside any method. In the .h file: class .... {
8
1533
by: ma740988 | last post by:
Consider // file fft_data.h class FFT_DATA { public: static const unsigned int Data_Words = 10; struct Data_Block_Type {
5
1316
by: JustSomeGuy | last post by:
I have a class that has a public data member. If however I declare that data member as being static, then the linker fails and says the symbol is undefined. Must static data members be private?
8
4604
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? =================
14
2887
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
2
3248
by: wkaras | last post by:
Section 9.4.2 paragraph 4 of the draft Standard says: If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant- initializer which shall be an integral constant expression (expr.const) In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the...
14
2825
by: John Ratliff | last post by:
I'm trying to find out whether g++ has a bug or not. Wait, don't leave, it's a standard C++ question, I promise. This program will compile and link fine under mingw/g++ 3.4.2, but fails to link under Linux/g++ 3.3.3. --------------------------------------------- #include <iostream> #include <utility>
9
1466
by: glue | last post by:
I have a class with a list member and the list seems to behave like it's static while other class members don't. The code... class A: name = "" data = def __init__(self, name): self.name = name def append(self, info): self.data.append(info)
6
2172
by: r.z. | last post by:
They should be initialized before any instance is created. I have no idea in which file, in which place should I put their initialization code to be sure they are initialize only once, before any instance is created. I cannot visualize the control flow of an executing program when the code is split in multiple files.
0
9928
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10718
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
10816
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
10402
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
9555
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7111
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
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
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.