Click here to Skip to main content
15,912,665 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generaloutlook embedding Pin
emmatty21-Feb-04 23:13
emmatty21-Feb-04 23:13 
GeneralGDI+ problem: Image and Bitmap Pin
Arcrest21-Feb-04 20:42
Arcrest21-Feb-04 20:42 
Generalcaps lock Pin
Archer28221-Feb-04 20:03
Archer28221-Feb-04 20:03 
GeneralRe: caps lock Pin
Ravi Bhavnani22-Feb-04 10:02
professionalRavi Bhavnani22-Feb-04 10:02 
GeneralMemory Management in C++ Pin
Iceberg7621-Feb-04 18:44
Iceberg7621-Feb-04 18:44 
GeneralRe: Memory Management in C++ Pin
Arcrest21-Feb-04 21:00
Arcrest21-Feb-04 21:00 
GeneralRe: Memory Management in C++ Pin
Keith Vitali22-Feb-04 0:09
Keith Vitali22-Feb-04 0:09 
GeneralRe: Memory Management in C++ Pin
Gary R. Wheeler22-Feb-04 4:43
Gary R. Wheeler22-Feb-04 4:43 
Essentially, there are three ways to create an object:
class Object {
public:
    Object();
    ~Object();
    // ...
};

Object GlobalObject;

void Function()
{
    Object StackObject;
    //...
    Object *HeapObject = new Object;
        //...
    delete HeapObject;
}
First, we have a class Object, with a constructor and a destructor. There are three instances of the class in the code: GlobalObject, StackObject, and HeapObject (which is a pointer).

Since GlobalObject is at file scope, the constructor for GlobalObject is called by the C++ runtime when the program starts, and its destructor when the program exits.

StackObject is created on the stack, inside the function Function. It's destructor is called when Function() exits. More precisely, the destructor is called when execution leaves the enclosing scope (the outermost braces "{" "}" of the function).

HeapObject is a pointer to an Object allocated on the heap using the new operator. In this case, the user (you the programmer) must explicitly destroy the object using the delete operator. The delete operator calls the destructor.

COM objects are slightly different. You 'allocate' COM objects using CoCreateInstance or one of the related functions, which return an interface pointer. All COM interfaces derive from the IUnknown interface, which has three methods: AddRef, Release, and QueryInterface. When you are done using a COM object, you release it by calling the Release method through its interface pointer.


Software Zen: delete this;
GeneralRe: Memory Management in C++ Pin
Iceberg7622-Feb-04 8:19
Iceberg7622-Feb-04 8:19 
GeneralRe: Memory Management in C++ Pin
Gary R. Wheeler22-Feb-04 15:38
Gary R. Wheeler22-Feb-04 15:38 
Generalhelp with classes Pin
GdsFisher21-Feb-04 18:00
GdsFisher21-Feb-04 18:00 
GeneralRe: help with classes Pin
wb21-Feb-04 18:26
wb21-Feb-04 18:26 
GeneralRe: help with classes Pin
Iceberg7621-Feb-04 18:29
Iceberg7621-Feb-04 18:29 
GeneralRe: help with classes Pin
GdsFisher22-Feb-04 6:19
GdsFisher22-Feb-04 6:19 
GeneralStrings in C++ Pin
Iceberg7621-Feb-04 16:14
Iceberg7621-Feb-04 16:14 
GeneralRe: Strings in C++ Pin
Tim Smith21-Feb-04 17:11
Tim Smith21-Feb-04 17:11 
GeneralRe: Strings in C++ Pin
Iceberg7621-Feb-04 19:28
Iceberg7621-Feb-04 19:28 
GeneralRe: Strings in C++ Pin
markkuk21-Feb-04 23:58
markkuk21-Feb-04 23:58 
GeneralRe: Strings in C++ Pin
Tim Smith22-Feb-04 4:13
Tim Smith22-Feb-04 4:13 
GeneralRe: Strings in C++ Pin
Michael Dunn22-Feb-04 5:48
sitebuilderMichael Dunn22-Feb-04 5:48 
GeneralRe: Strings in C++ Pin
Tim Smith22-Feb-04 6:34
Tim Smith22-Feb-04 6:34 
GeneralRe: Strings in C++ Pin
Michael Dunn22-Feb-04 6:40
sitebuilderMichael Dunn22-Feb-04 6:40 
GeneralRe: Strings in C++ Pin
Iceberg7622-Feb-04 9:49
Iceberg7622-Feb-04 9:49 
GeneralRe: Strings in C++ Pin
Tim Smith22-Feb-04 11:16
Tim Smith22-Feb-04 11:16 
GeneralRe: Strings in C++ Pin
Tim Smith22-Feb-04 4:15
Tim Smith22-Feb-04 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.