青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

asm, c, c++ are my all
-- Core In Computer
posts - 139,  comments - 123,  trackbacks - 0

/********************************************\
|????歡迎轉載, 但請保留作者姓名和原文鏈接, 祝您進步并共勉!???? |
\********************************************/


C++對象模型(5) -? Copy Constructor Construction

作者: Jerry Cat
時間: 2006/05/05
鏈接:?http://m.shnenglu.com/jerysun0818/archive/2006/05/05/6632.html



2.2 Copy Constructor Construction
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

引子:
Say the class designer explicitly defines a copy constructor (a constructor requiring a single
argument of its class type), such as either of the following:
// examples of user defined copy constructors may be multi-argument provided each second
// and subsequent argument is provided with a default value
X::X( const X& x );
Y::Y( const Y& y, int = 0 );

In this case, that constructor is invoked, under most circumstances, in each program instance
where initialization of one class object with another occurs. This may result in the generation
of a temporary class object or the actual transformation of program code (or both).

1). Default Memberwise Initialization(注:對內建成員對象可遞歸用之):
bitwise:按位
memberwise:逐成員
What if the class does not provide an explicit copy constructor? Each class object initialized
with another object of its class is initialized by what is called default memberwise initialization.
Default memberwise initialization copies the value of each built-in or derived data member (such
as a pointer or an array) from the one class object to another. A member class object, however,
is not copied; rather, memberwise initialization is recursively applied. For example, consider
the following class declaration:

class String {
public:
?? // ... no explicit copy constructor
private:
?? char *str;
?? int?? len;
};
The default memberwise initialization of one String object with another, such as

String noun( "book" );
String verb = noun;
is accomplished as if each of its members was individually initialized in turn:

// semantic equivalent of memberwise initialization
verb.str = noun.str;
verb.len = noun.len;
If a String object is declared as a member of another class, such as the following:

class Word {
public:
?? // ...no explicit copy constructor
private:
?? int???? _occurs;
?? String? _word;
};
then the default memberwise initialization of one Word object with another copies the value of
its built-in member _occurs and then recursively applies memberwise initialization to its class
String member object _word.

How is this operation in practice carried out? The original ARM tells us:
??? Conceptually, for a class X [this operation is] implemented by…a copy constructor.

The operative word here is conceptually. The commentary that follows explains:
??? In practice, a good compiler can generate bitwise copies for most class objects since they
??? have bitwise copy semantics….

That is, a copy constructor is not automatically generated by the compiler for each class that
does not explicitly define one. Rather, as the ARM tells us:
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? 缺省者按需也
??? Default constructors and copy constructors…are generated (by the compiler) where needed.

Needed in this instance means when the class does not exhibit bitwise copy semantics.(此處的
"needed"意味著彼時該類并無按位拷貝之意). The Standard retains the meaning of the ARM, while
formalizing its discussion (my comments are inserted within parentheses) as follows:

??? A class object can be copied in two ways, by initialization (what we are concerned with
??? here)…and by assignment (treated in Chapter 5). Conceptually (my italics), these two
??? operations are implemented by a copy constructor and copy assignment operator.

As with the default constructor, the Standard speakers of an implicitly declared and implicitly
defined copy constructor if the class does not declare one. As before, the Standard distinguishes
between a trivial and nontrivial copy constructor. It is only the nontrivial instance that in
practice is synthesized within the program(實際上只有"并非不重要"的拷貝構造函數才會被合成進程序
中). The criteria for determining whether a copy constructor is trivial is whether the class
exhibits bitwise copy semantics(類只對"不重要"的拷貝構造函數進行按位拷貝). In the next
section, I look at what it means to say that a class exhibits bitwise copy semantics.

2). Bitwise Copy Semantics:

In the following program fragment:
#include "Word.h"
Word noun( "block" );

void foo()
{
?? Word verb = noun;
?? // ...
}
it is clear that verb is initialized with noun. But without looking at the declaration of class
Word, it is not possible to predict the program behavior of that initialization. If the designer
of class Word defines a copy constructor, the initialization of verb invokes it. If, however,
the class is without an explicit copy constructor, the invocation of a compiler-synthesized
instance depends on whether the class exhibits bitwise copy semantics. For example, given the
following declaration of class Word:

// declaration exhibits bitwise copy semantics
class Word {
public:
?? Word( const char* );
?? ~Word() { delete [] str; }
?? // ...
private:
?? int?? cnt;
?? char *str;
};

a default copy constructor need not be synthesized, since the declaration exhibits bitwise copy
semantics, and the initialization of verb need not result in a function call.
However, the following declaration of class Word does not exhibit bitwise copy semantics:
此時的按位拷貝將是致命的, 最好應顯式定義一拷貝構造函數, 用new從堆中另辟內存塊空間.
??? Of course, the program fragment will execute disastrously given this declaration of class
??? Word. (Both the local and global object now address the same character string. Prior to
??? exiting foo(), the destructor is applied to the local object, thus the character string is
??? deleted. The global object now addresses garbage.) The aliasing problem with regard to member
??? str can be solved only by overriding default memberwise initialization with an explicit copy
??? constructor implemented by the designer of the class (or by disallowing copying altogether).
??? This, however, is independent of whether a copy constructor is synthesized by the compiler.

// declaration does not exhibits bitwise copy semantics
class Word {
public:
?? Word( const String& );
?? ~Word();
?? // ...
private:
?? int??? cnt;
?? String str;
};
where String declares an explicit copy constructor:

class String {
public:
?? String( const char * );
?? String( const String& );
?? ~String();
?? // ...
};
In this case, the compiler needs to synthesize a copy constructor in order to invoke the copy
constructor of the member class String object:

// A synthesized copy constructor (Pseudo C++ Code)
inline Word::Word( const Word& wd )
{
?? str.String::String( wd.str );
?? cnt = wd.cnt;
}
It is important to note that in the case of the synthesized copy constructor, the nonclass
members of types such as integers, pointers, and arrays are also copied, as one would expect.

3). Bitwise Copy Semantics Not! (此時)對按位拷貝大聲說不!

When are bitwise copy semantics not exhibited by a class? There are four instances:

(1). When the class contains a member object of a class for which a copy constructor exists
???? (either explicitly declared by the class designer, as in the case of the previous String
???? class, or synthesized by the compiler, as in the case of class Word)
(2). When the class is derived from a base class for which a copy constructor exists (again,
???? either explicitly declared or synthesized)
(3). When the class declares one or more virtual functions
(4). When the class is derived from an inheritance chain in which one or more base classes are virtual

4). Resetting the Virtual Table Pointer:

Recall that two program "augmentations" occur during compilation whenever a class declares one
or more virtual functions.

??? A virtual function table that contains the address of each active virtual function associated
??? with that class (the vtbl) is generated.

??? A pointer to the virtual function table is inserted within each class object (the vptr).

Obviously, things would go terribly wrong if the compiler either failed to initialize or
incorrectly initialized the vptr of each new class object. Hence, once the compiler introduces
a vptr into a class, the affected class no longer exhibits bitwise semantics. Rather, the
implementation now needs to synthesize a copy constructor in order to properly initialize the
vptr. Here's an example.

First, I define a two-class hierarchy of ZooAnimal and Bear:
class ZooAnimal {
public:
?? ZooAnimal();
?? virtual ~ZooAnimal();

?? virtual void animate();
?? virtual void draw();
?? // ...
private:
?? // data necessary for ZooAnimal's version of animate() and draw()
};

class Bear : public ZooAnimal {
public:
?? Bear();

?? void animate();
?? void draw();
?? virtual void dance();
?? // ...
private:
?? // data necessary for Bear's version of animate(), draw(), and dance()
};
The initialization of one ZooAnimal class object with another or one Bear class object with
another is straightforward and could actually be im-plemented with bitwise copy semantics (apart
from possible pointer member aliasing, which for simplicity is not considered). For example, given

Bear yogi;
Bear winnie = yogi;
yogi is initialized by the default Bear constructor. Within the constructor, yogi's vptr is
initialized to address the Bear class virtual table with code inserted by the compiler. It is
safe, therefore, to simply copy the value of yogi's vptr into winnie's. The copying of an
object's vptr value, however, ceases to be safe when an object of a base class is initialized
with an object of a class derived from it. For example, given

ZooAnimal franny = yogi;
the vptr associated with franny must not be initialized to address the Bear class virtual table
(which would be the result if the value of yogi's vptr were used in a straightforward bitwise
copy此時就不能按位拷貝). Otherwise the invocation of draw() in the following program fragment
would blow up when franny were passed to it:
??? [5]The draw() virtual function call through franny must invoke the ZooAnimal instance rather
??? than the Bear instance, even though franny is initialized with the Bear object yogi because
??? franny is a ZooAnimal object. In effect, the Bear portion of yogi is sliced off when franny
??? is initialized.
??? 虛擬語句(下面是多態的情況, 指針或引用):
??? ---------------------------------------
??? Were franny declared a reference (or were it a pointer initialized with the address of
??? yogi), then invocations of draw() through franny would invoke the Bear instance. This is
??? discussed in Section 1.3

void draw( const ZooAnimal& zoey ) { zoey.draw(); }
void foo() {
// franny's vptr must address the ZooAnimal virtual table not the Bear virtual table yogi's vptr addresses
????? ZooAnimal franny = yogi;

????? draw( yogi );?? // invoke Bear::draw()
????? draw( franny ); // invoke ZooAnimal::draw()
?? }
That is, the synthesized ZooAnimal copy constructor explicitly sets the object's vptr to the
ZooAnimal class virtual table rather than copying it from the right-hand class object.

5). Handling the Virtual Base Class Subobject:

The presence of a virtual base class also requires special handling. The initialization of one
class object with another in which there is a virtual base class subobject also invalidates
bitwise copy semantics. 又一次讓按位拷貝失效.

Each implementation's support of virtual inheritance involves the need to make each virtual base
class subobject's location within the derived class object available at runtime. Maintaining the
integrity of this location is the compiler's responsibility. Bitwise copy semantics could result
in a corruption of this location, so the compiler must intercede with its own synthesized copy
constructor. For example, in the following declaration, ZooAnimal is derived as a virtual base
class of Raccoon:

class Raccoon : public virtual ZooAnimal {
public:
?? Raccoon()????????? { /* private data initialization */ }
?? Raccoon( int val ) { /* private data initialization */ }
?? // ...
private:
?? // all necessary data
};
Compiler-generated code to invoke ZooAnimal's default constructor, initialize Raccoon's vptr,
and locate the ZooAnimal subobject within Raccoon is inserted as a prefix within the two Raccoon
constructors.

What about memberwise initialization? The presence of a virtual base class invalidates bitwise
copy semantics. (這才是Again, the problem is not when one object of a class is initialized with
a second object of the same exact class. It is when an object is initialized with an object of
one of its derived classes問題的實質). For example, consider the case in which a Raccoon object
is initialized with a RedPanda object, where RedPanda is declared as follows:

class RedPanda : public Raccoon {
public:
?? RedPanda()????????? { /* private data initialization */ }
?? RedPanda( int val ) { /* private data initialization */ }
?? // ...
private:
?? // all necessary data
};
Again, in the case of initializing one Raccoon object with another, simple bitwise copy is sufficient:

// simple bitwise copy is sufficient
Raccoon rocky;
Raccoon little_critter = rocky;

However, an attempt to initialize little_critter with a RedPanda object requires the compiler
to intercede, if subsequent programmer attempts to access its ZooAnimal subobject are to execute
properly (not an unreasonable programmer expectation!):(上一行are的主語是attempts)

// simple bitwise copy is not sufficient, compiler must explicitly initialize little_critter's
// virtual base class pointer/offset
RedPanda?? little_red;
Raccoon??? little_critter = little_red;

To summarize: We have looked at the four conditions under which bitwise copy semantics do not
hold for a class and the default copy constructor, if undeclared, is considered nontrivial
(在那四種不應進行按位拷貝的情形, 設計者如未申明則缺省的構造函數還是很有必要的). Under these
conditions, the compiler, in the absence of a declared copy constructor, must synthesize a copy
constructor in order to correctly implement the initialization of one class object with another.
In the next section, the implementation strategies for invoking the copy constructor and how
those strategies affect our programs are discussed.

posted on 2006-05-05 03:41 Jerry Cat 閱讀(1033) 評論(0)  編輯 收藏 引用

<2006年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用鏈接

留言簿(7)

隨筆檔案

最新隨筆

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久综合中文| 一区二区久久久久久| 亚洲精品乱码久久久久久| 国产真实乱偷精品视频免| 国产亚洲aⅴaaaaaa毛片| 国产视频观看一区| 国内精品久久久久久久影视蜜臀| 国产一区二区视频在线观看| 狠狠色综合色区| 在线国产日韩| 中文日韩欧美| 欧美一二三视频| 久色婷婷小香蕉久久| 欧美激情二区三区| 久久国产精品72免费观看| 在线成人免费视频| 亚洲激情中文1区| 在线视频欧美精品| 久久成人免费电影| 久久亚洲综合色| 国产女精品视频网站免费 | 欧美日韩成人在线视频| 国产精品入口夜色视频大尺度 | 国产精品久久久久秋霞鲁丝| 国产亚洲精品高潮| 亚洲人成在线免费观看| 亚洲欧美成人一区二区三区| 久久一区国产| 99视频精品| 久久久久国色av免费观看性色| 欧美国产第二页| 国产美女在线精品免费观看| 国内视频一区| 亚洲视频在线免费观看| 久久综合狠狠综合久久激情| 99这里有精品| 久久综合给合| 国产小视频国产精品| 一区二区三区视频在线观看| 免费在线日韩av| 亚洲欧美精品中文字幕在线| 欧美劲爆第一页| 一区在线免费观看| 欧美一区二区在线免费播放| 日韩视频免费观看| 理论片一区二区在线| 国产婷婷色综合av蜜臀av | 久久嫩草精品久久久精品| 亚洲精品永久免费| 欧美大片免费| 激情久久综艺| 欧美在线视频网站| 一区二区三区高清视频在线观看| 久久亚洲精品一区| 狠狠做深爱婷婷久久综合一区| 正在播放欧美视频| 亚洲国产免费看| 久久不见久久见免费视频1| 国产精品福利影院| 亚洲女人天堂成人av在线| 日韩午夜在线电影| 欧美人成免费网站| 99精品久久久| 亚洲毛片一区二区| 欧美日韩中文字幕| 亚洲伊人久久综合| 在线亚洲自拍| 国产九色精品成人porny| 欧美一区二区三区四区在线| 亚洲欧美三级伦理| 国产一区导航| 久久青青草原一区二区| 久久精品国产亚洲aⅴ| 在线成人黄色| 亚洲巨乳在线| 亚洲日韩欧美视频| 嫩模写真一区二区三区三州| 最近中文字幕日韩精品| 亚洲激情在线观看| 欧美视频精品在线观看| 亚洲亚洲精品在线观看| 亚洲一区免费看| 国产日韩一区| 免费日本视频一区| 欧美日韩理论| 欧美伊久线香蕉线新在线| 久久久久久久久蜜桃| 亚洲缚视频在线观看| 亚洲欧洲日本在线| 国产欧美日韩在线视频| 蜜桃av一区| 欧美日韩xxxxx| 欧美在线观看网站| 男女精品视频| 亚洲欧美激情一区| 久久频这里精品99香蕉| 在线亚洲伦理| 久久综合久久美利坚合众国| 亚洲色诱最新| 久久久99国产精品免费| 99精品视频免费观看| 午夜精品国产精品大乳美女| 亚洲激情在线观看| 性欧美大战久久久久久久免费观看| 亚洲成人资源网| 艳女tv在线观看国产一区| 激情欧美一区二区| av不卡在线| 1024亚洲| 亚洲午夜av在线| 亚洲精品久久久蜜桃| 久久爱www久久做| 亚洲一区二区三区色| 久久夜色精品亚洲噜噜国产mv| 亚洲在线一区二区三区| 女主播福利一区| 久久精品电影| 欧美无砖砖区免费| 日韩视频免费在线观看| 亚洲精品1234| 久久精品成人| 欧美在线视频免费| 国产精品久久久久影院亚瑟| 亚洲大片免费看| 一区二区亚洲精品国产| 羞羞漫画18久久大片| 亚洲一区二区视频| 欧美精品一区二区在线观看| 欧美 日韩 国产 一区| 国产香蕉久久精品综合网| 在线亚洲激情| 亚洲深夜福利网站| 欧美高清视频一区二区三区在线观看| 久久精品中文字幕一区| 国产精品亚洲综合色区韩国| 一本色道久久88综合日韩精品| 亚洲美女视频网| 久久资源av| 欧美激情第10页| 亚洲啪啪91| 日韩视频一区二区| 一区二区久久| 欧美四级在线| 一区二区三区高清视频在线观看| 一区二区日韩伦理片| 欧美日韩卡一卡二| 亚洲一区二区精品在线| 欧美一级在线播放| 国产视频欧美| 久久成人免费日本黄色| 久久视频免费观看| 精品av久久久久电影| 久久久亚洲综合| 亚洲国产欧美一区二区三区同亚洲 | 日韩午夜在线观看视频| 欧美精品二区三区四区免费看视频| 欧美国产日韩一二三区| 亚洲黄色精品| 欧美日韩成人激情| 亚洲一区二区黄| 美日韩精品视频| 日韩视频第一页| 国产精品乱人伦中文| 久久久精品动漫| 91久久精品视频| 午夜伦理片一区| 黄色国产精品一区二区三区| 猫咪成人在线观看| 亚洲视频久久| 媚黑女一区二区| 一区二区不卡在线视频 午夜欧美不卡在 | 久久亚洲视频| 日韩午夜三级在线| 国产欧美日韩亚洲| 免费欧美日韩国产三级电影| 日韩一级精品| 久久人人爽爽爽人久久久| 亚洲精品人人| 国产尤物精品| 欧美午夜精品电影| 久久综合九色综合欧美狠狠| 一区二区三区四区国产精品| 欧美3dxxxxhd| 欧美在线视频在线播放完整版免费观看| 在线成人国产| 国产欧美在线看| 欧美日韩视频专区在线播放| 久久久伊人欧美| 亚洲一区美女视频在线观看免费| 欧美大片va欧美在线播放| 亚洲女优在线| 日韩图片一区| 精品999久久久| 国产日韩精品一区二区| 欧美视频久久| 欧美精品日韩www.p站| 久久精品一区蜜桃臀影院| 亚洲综合国产激情另类一区| 欧美国产专区| 麻豆乱码国产一区二区三区|