Solved

static member of uninstantiated class

Posted on 2001-06-12
8
306 Views
Last Modified: 2008-02-26
Is a static member of a class without any instances ever constructed?

If so, when? And when is it destroyed?
0
Comment
Question by:glebspy
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6180230
you have to construct it from another part of code (for example in InitInstance, or other init-functions) And you're responsible for freeing this member-object after use.

So the answer to your question is: no, it is not constructed.
0
 
LVL 1

Author Comment

by:glebspy
ID: 6180250
Even if it is declared outside of the class scope i.e.

file1:

class no_Instance{
  static int member;};


file2:
int no_Instance::member(5);

link main file1 file2

???

Is the behaviour for referring to member in main undefined, or universally error-producing at run-time or compile-time?
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6180277
if you initialize it outside as you do, there is no problem. all init outside code is made by programm start.
you cansee it by setting a break point to the line in file2 and debug.
0
 
LVL 1

Author Comment

by:glebspy
ID: 6180300
In such circumstances, when exactly is it destroyed?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Accepted Solution

by:
missionImpossible earned 50 total points
ID: 6180308
it is destroyed when you end your programm.
if your static member is a flat member (not a pointer) you need not to free its memory, it is done by environment. Otherwise you have to delete it by your code.
0
 
LVL 2

Expert Comment

by:GloriousRain
ID: 6180311
<MSDN>
Static Data Members
Classes can contain static member data and member functions. When a data member is declared as static, only one copy of the data is maintained for all objects of the class. (For more information, see Static Member Functions.)

Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope. These static members have external linkage. The following example illustrates this:

class BufferedOutput
{
public:
    // Return number of bytes written by any object of this class.
    short BytesWritten() { return bytecount; }

    // Reset the counter.
    static void ResetCount() { bytecount = 0; }

    // Static member declaration.
    static long bytecount;
};

// Define bytecount in file scope.
long BufferedOutput::bytecount;

In the preceding code, the member bytecount is declared in class BufferedOutput, but it must be defined outside the class declaration.

Static data members can be referred to without referring to an object of class type. The number of bytes written using BufferedOutput objects can be obtained as follows:

long nBytes = BufferedOutput::bytecount;

For the static member to exist, it is not necessary that any objects of the class type exist. Static members can also be accessed using the member-selection (. and ?>) operators. For example:

BufferedOutput Console;

long nBytes = Console.bytecount;

In the preceding case, the reference to the object (Console) is not evaluated; the value returned is that of the static object bytecount.

Static data members are subject to class-member access rules, so private access to static data members is allowed only for class-member functions and friends. These rules are described in Chapter 10, Member-Access Control. The exception is that static data members must be defined in file scope regardless of their access restrictions. If the data member is to be explicitly initialized, an initializer must be provided with the definition.

The type of a static member is not qualified by its class name. Therefore, the type of BufferedOutput::bytecount is long.


--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
<MSDN>
0
 
LVL 1

Author Comment

by:glebspy
ID: 6180332
Good answer.

So I suppose, if member is an object *containing* a pointer, then whether or not that pointer is deallocated depends on what I've put in Member's destructor..?
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6180514
what do you mean with members destructor?
if your memer is an pointer, you have to delete it from another peace of code (for example: you can delete your static member in the ExitInstance() or destructor of your mainframe.

if you do so, the static members destructor is then called.

Hope I could helped.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now