Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

static member of uninstantiated class

Posted on 2001-06-12
8
Medium Priority
?
344 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
Technology Partners: 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!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

609 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