What about defining a class, but not making it an instance?

I am learning COM code, and the following code puzzled me.

STDMETHODIMP _(ULONG) ComCar::Release() { if(--m_refCount==0) { delete this; // how could this "suicide" deletion be possible? return 0; } return m_refCount; } 

Yes. this is similar code from here . And there I ask how the memeber method deletes the object belonging to it. Now I think of these two scenarios.

1- If I define a class without instantiating it. Is there any data related to this type at runtime?

2- If I make only one instance of the class and make the single object itself commit suicide through the above code. After deleting an object, where can the methods of the object remain? Or am I paying too much attention to the illusion of encapsulation?

I am wondering if the class methods are running and then stored in the code / text segment of the program, regardless of the existence of any object of its type. Therefore, class methods exist as long as you define them.

+4
source share
2 answers

1- If I define a class without instantiating it. Will this type exist at run time?

Edit post OP edit and gnud comment:

A class is a user-defined type. The type will be available as a float type, even if you are not using any kind of float in your code. Type information will be present. In particular, since gnud indicates if it is an abstract base class, you cannot create any objects of this type, but they have derived objects of the class. Information about a member of the base class will be appropriately copied / updated to objects of the derived class (provided that you have the corresponding ctors defined, of course).

2- If I make only one instance of the class and make a very single object commit suicide using the above code. After deleting the object, where can the method stay?

Methods are parts of executable code. Class objects have a table of all member function pointers. This table is updated when an object is created to point to the corresponding area of ​​the binary file. When an object is deleted, the binary remains without access to it.

Edit: More on delete this , which is completely legal: this is FAQ 16.15 . Also, note that this is only useful in very few cases - the reference counting object is one of those (as shown in your code).

+2
source

The code for a member function is associated with a class, not an instance of the class. There is one copy of the code, regardless of how many instances are created (zero, one, a million, etc.) Doing delete this; destroys the instance, but it does nothing for the code for class functions.

1: The this pointer exists only in a non-static member function, so the code will not compile unless it is part of such a function. It must be called by the instance. If there is nothing in the program that would create an instance of the class, then the function does not call anything, but the code for the function still exists at run time; it just does not execute.

2: Class methods are stored in the text segment of the program, and not in the data segment, as you might have guessed. They stay there for a lifetime program. (In fact, the text segment is usually read-only and cannot be changed at run time.)

+2
source

Source: https://habr.com/ru/post/1302191/


All Articles