开发者

How about defining a class but not make any instance of it?

开发者 https://www.devze.com 2022-12-21 05:31 出处:网络
I am learning some COM code and the following code puzzled me. STDMETHODIMP _(ULONG) ComCar::Release()

I am learning some 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 the similar code from here. And there I askes about how could a memeber method delete its belonging object. Now I am thinking about these 2 scenarios.

1- If I define a class withou开发者_开发知识库t making an instance of it. Would any data related to this type exist in runtime?

2- If I only make 1 instance of the class and make the very single object commit suicide via the above code. After the object is deleted, where could the object's methods stay? Or am I paying too much attention to the encapsulation illusion?

I am wondering whether class methods are first name-mangled and then stored in the code/text segment of the program without regard to the existence of any object of its type. So the class methods exist as long as you define them.


1- If I define a class without making an instance of it. Would this type exist in runtime?

Edit post OP's edit and gnud's comment:

A class is an user defined type. The type will be available as will be a float type even if you don't use any float in your code. The type information will be present. Particularly as gnud points out, if this is an abstract base class, you'd not be able to create any objects of that type but have derived class objects. The base class's member information will be suitably copied/updated to the derived class objects (provided you have appropriate ctors defined of course).

2- If I only make 1 instance of the class and make the very single object commit suicide via the above code. After the object is deleted, where could the method stay?

Methods are portions of executable code. Class objects have a table of all member function pointers. This table is updated when the object is created to point to the appropriate region of the binary. When the object is deleted, the binary remains, without a way to access it.

Edit: More on delete this which is perfectly legal: This is FAQ 16.15. Further, note that this is useful only in very few instances -- a reference counted object is one such (as you show in your code).


The code for a member function is associated with the class, not with an instance of the class. There's one copy of the code, regardless of how many instances are created (zero, one, a million, whatever.) Doing delete this; destroys an instance, but it doesn't do anything at all to the code for the class's member functions.

1: The this pointer only exists in a non-static member function, so that code won't compile unless it's part of such a function. It must be called on an instance. If there's nothing in the program that creates an instance of the class, there's nothing that calls the function, but the code for the function still exists at runtime; it just doesn't get executed.

2: Class methods are stored in the program's text segment, not its data segment, as you guessed. They remain there for the lifetime of the program. (In fact, the text segment is typically read-only and cannot be modified at runtime.)

0

精彩评论

暂无评论...
验证码 换一张
取 消