Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

ANSI/ISO C++ Professional Programmer''''s Handbook phần 4 pptx
MIỄN PHÍ
Số trang
25
Kích thước
76.2 KB
Định dạng
PDF
Lượt xem
792

ANSI/ISO C++ Professional Programmer''''s Handbook phần 4 pptx

Nội dung xem thử

Mô tả chi tiết

Covariance of Virtual Member Functions

The implementation of virtual constructors relies on a recent modification to C++, namely virtual functions' covariance.

An overriding virtual function has to match the signature and the return type of the function it overrides. This restriction

was recently relaxed to enable the return type of an overriding virtual function to co-vary with its class type. Thus, the

return type of a public base can be changed to the type of a derived class. The covariance applies only to pointers and

references.

CAUTION: Please note that some compilers do not support virtual member functions' covariance yet.

Assignment Operator

A user-declared assignment operator of class C is a nonstatic, nontemplate member function of its class, taking exactly

one argument of type C, C&, const C&, volatile C&, or const volatile C&.

Implicitly-Defined Assignment Operator

If there is no user-defined assignment operator for a class, the implementation implicitly declares one. An

implicitly-declared assignment operator is an inline public member of its class, and it has the form

C& C::operator=(const C&);

if each base class of C has an assignment operator whose first argument is a reference to a const object of base class

type, and if all the nonstatic embedded objects in C also have an assignment operator that takes a reference to a const

object of their type. Otherwise, the implicitly-declared assignment operator is of the following type:

C& C::operator=(C&);

An implicitly-declared assignment operator has an exception specification. The exception specification contains all the

exceptions that might be thrown by other special functions that the assignment operator invokes directly. An assignment

operator is said to be trivial if it is implicitly declared, if its class has no virtual member functions or virtual base classes,

and if its direct base classes and embedded objects have a trivial assignment operator.

Simulating Inheritance Of Assignment Operator

Because an assignment operator is implicitly declared for a class if it is not declared by the programmer, the assignment

operator of a base class is always hidden by the assignment operator of a derived class. In order to extend -- rather than

override -- the assignment operator in a derived class, you must first invoke the assignment operator of the base

explicitly, and then add the operations that are required for the derived class. For example

class B

{

private:

char *p;

public:

enum {size = 10};

const char * Getp() const {return p;}

B() : p ( new char [size] ) {}

B& operator = (const C& other);

{

if (this != &other)

ANSI/ISO C++ Professional Programmer's Handbook - 4 - Special Mem...nstructor, Copy Constructor, Destructor, And Assignment Operator

file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (17 von 24) [12.05.2000 14:46:07]

strcpy(p, other.Getp() );

return *this;

}

};

class D : public B

{

private:

char *q;

public:

const char * Getq() const {return q;}

D(): q ( new char [size] ) {}

D& operator = (const D& other)

{

if (this != &other)

{

B::operator=(other); //first invoke base's assignment operator explicitly

strcpy(q, (other.Getq())); //add extensions here

}

return *this;

}

};

When Are User-Written Copy Constructors And

Assignment Operators Needed?

The synthesized copy constructor and assignment operator perform a memberwise copy. This is the desirable behavior

for most uses. However, it can be disastrous for classes that contain pointers, references, or handles. In such cases, you

have to define a copy constructor and assignment operator to avoid aliasing. Aliasing occurs when the same resource is

used simultaneously by more than one object. For example

#include <cstdio>

using namespace std;

class Document

{

private:

FILE *pdb;

public:

Document(const char *filename) {pdb = fopen(filename, "t");}

Document(FILE *f =NULL) : pdb{}

~Document() {fclose(pdb);} //bad, no copy constructor

//or assignment operator defined

};

void assign(Document& d)

{

Document temp("letter.doc");

d = temp; //Aliasing; both d and temp are pointing to the same file

}//temp's destructor is now called and closes file while d is still using it

int main()

{

Document doc;

assign(doc);

ANSI/ISO C++ Professional Programmer's Handbook - 4 - Special Mem...nstructor, Copy Constructor, Destructor, And Assignment Operator

file:///D|/Cool Stuff/old/ftp/1/1/ch04/ch04.htm (18 von 24) [12.05.2000 14:46:07]

Tải ngay đi em, còn do dự, trời tối mất!