More Effective C ++ 27

xiaoxiao2021-04-08  378

Terms 27: Requirements (or prohibited) objects are produced in HEAP

Request objects are generated in Heap HEAP-BASED OBJECTS

Consider the following code:

Class

HeapClass

{

public

:

Void

Destory

()

Const

{

Delete

THIS

}

Private

:

~

HeapClass

() {}

}

HeapClass

*

Ptr

=

New

HeapClass

;

Ptr

->

Destory

();

Such calls are really very powerful, and they don't generate non-HEAP objects.

I don't want to say more about the inheritance and combination, but I said it is more interesting.

Determine if an object is within HEAP

Consider the following code:

New

HeapClass

(*

New

HeapClass

);

What do you think of the compiler should do?

Calling Operator New

2. Call constructor

3. Call the second Operator New

4. Call the second constructor

But it can be surprised that the compiler does not do its commitment, so the actual implementation may be

Calling Operator New

2. Call the second Operator New

3. Call constructor

4. Call the second constructor

I know that VC6 is implemented.

Class

HeapClass

{

Private

:

Void

*

Operator

New

[]

Size_t

Size

);

Typedef

Const

Void

*

Rawaddress

;

Void

Operator

Delete

[]

Void

*

Ptr

);

public

:

Void

Operator

Delete

(

Void

*

Ptr

)

{

PRINTF

(

"delete / n"

);

::

Operator

Delete

(

Ptr

);

m_address

.

Erase

(

STD

::

Remove

(

m_address

.

Begin

(),

m_address

.

end

(),

Ptr

),

m_address

.

end

())

Return

;

}

Void

*

Operator

New

(

Size_t

Size

)

{

PRINTF

(

"new / n"

);

Void

*

Ptr

= ::

Operator

New

(

Size

);

m_address

.

Push_back

(

Ptr

);

Return

Ptr

;

}

HeapClass

()

{

PRINTF

(

"Constructor! / N"

);

}

HeapClass

(

Const

HeapClass

&)

{

Printf ("Copy Constructor! / N");

}

Virtual

Void

Destory

()

Const

{

Delete

THIS

}

Virtual

~

HeapClass

() = 0;

Bool

Isonheap

()

Const

{

// const void * Rawaddress = Dynamic_Cast (this);

Const

Void

*

Rawaddress

=

Const

Void

*)

THIS

);

STD

::

DEQUE

<

Rawaddress

> ::

Iteratoriter

=

STD

::

Find

(

m_address

.

Begin

(),

m_address

.

end

(),

Rawaddress

);

Return

iTer

! =

m_address

.

end

();

}

Private

:

Static

STD

::

DEQUE

<

Rawaddress

>

m_address

;

}

HeapClass

:: ~

HeapClass

() {}

STD

::

DEQUE

<

HeapClass

::

Rawaddress

>

HeapClass

::

m_address

;

Class

DHEAPCLASS

:

public

HeapClass

{

}

I wrote this Demo in VC6 test, but const void * rawaddress = Dynamic_cast (this); there will be an exception, which makes me feel very depressed, so this demo can only support ordinary inheritance mode, Many inheritance and virtual inheritance are not supported.

Prohibited objects are produced in HEAP

Consider the following code

Class

HeapClass

{

Private

:

Void

*

Operator

New

(

Size_t

Size

);

Void

*

Operator

New

[]

Size_t

Size

);

Void

Operator

Delete

(

Void

*

Ptr

);

Void

Operator

Delete

[]

Void

*

Ptr

);

public

:

HeapClass

() {

PRINTF

(

"Constructor! / N"

}

HeapClass (ConstheapClass &) {Printf ("Copy Constructor! / N");

public

:

~

HeapClass

() {}

}

This is indeed a relatively simple matter.

转载请注明原文地址:https://www.9cbs.com/read-132851.html

New Post(0)