Solved

static member of uninstantiated class

Posted on 2001-06-12
8
316 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:glebspy
ID: 6180300
In such circumstances, when exactly is it destroyed?
0
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 73
Handle Exceptions during instantiation 28 423
passing data from one form to another form in c++ 27 83
Precision Problem in C++ 7 44
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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

829 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