?
Solved

static member of uninstantiated class

Posted on 2001-06-12
8
Medium Priority
?
333 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:glebspy
ID: 6180300
In such circumstances, when exactly is it destroyed?
0
 
LVL 2

Accepted Solution

by:
missionImpossible earned 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

765 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