航行日记

欢迎光临水精灵的小屋
数据加载中……

C++学习笔记一类的构造和析购函数

重新学习一下最常见的类的构造函数和析构函数,也许会有新的收获:)
1. Constructors cannot return values, even if they have return statements. Specifying a constructor with a return type is an error, as is taking the address of a constructor.
构造函数不能返回类型,即使只有return声明语句也不行。指定一个构造函数的返回类型或它的地址都是错误的。
试了一下,效果这样:
class A
{
 public:
  A ();
};
A::A()
{
  return ;  //ok, equal to exit;no return type.
  return void; //error
  return 0;  //error
}

2. If a class has a constructor, each object of that type is initialized with the constructor prior to use in a program.
如果类有一个构造器,那么这个类的每个对象都应该在程序使用前先初始化构造函数。

Constructors are called at the point an object is created. Objects are created as:
Global (file-scoped or externally linked) objects.
Local objects, within a function or smaller enclosing block.
Dynamic objects, using the new operator. The new operator allocates an object on the program heap or “free store.”Temporary objects created by explicitly calling a constructor.
Temporary objects created implicitly by the compiler.
Data members of another class. Creating objects of class type, where the class type is composed of other class-type variables, causes each object in the class to be created.
Base class subobject of a class. Creating objects of derived class type causes the base class components to be created.
构造函数在对象生成时调用,以下情况下生成对象:
全局对象;局部对象;用new在堆上动态生成的对象;外部显式调用生成的临时对象;编译器隐式调用生成的临时对象;作为另一个类的数据成员;另一个类的基类。

3. The default method of initialization is to perform a bit-for-bit copy from the initializer into the object to be initialized. This technique is applicable only to:
Objects of built-in types. For example: int i = 100;
Pointers. For example: int i ; int *pi = &i;
References. For example: String sFileName("FILE.DAT"); String &rs = sFileName;
Objects of class type, where the class has no private or protected members, no virtual functions, and no base classes. For example:struct Point{ int x,y;}; Point pt = (10, 20); //static storage class only
缺省初始化方法是执行位拷贝。只适用于:内建类型;指针;引用;没有私有成员或保护成员,没有虚函数,并且没有基类的类对象。
什么情况下需要对类定义自拷贝函数和重载赋值函数?
答:当成员变量不允许bitwise的情况下,需要 memberwis。比如string, list, vetor等。

什么情况下析构函数应该声明为虚函数?
答:当类用作基类时,析构函数必须定义为虚函数。否则,派生类对象无法调用自身的析构函数。

posted on 2004-11-19 21:57 水精灵 阅读(513) 评论(6)  编辑 收藏 所属分类: C++语言

评论

#1楼    回复  引用    

现在是大四的学生了.眼看就要毕业了,想突击一下vc++,不知道这样可以吗?对了,我现在的专业是信息与计算科学.希望可以得到您的消息!
2004-11-24 10:26 | 阿苦 [未注册用户]

#2楼 [楼主]   回复  引用  查看    

VC覆盖的内容太庞杂,一时无法突击出成效,还是要通过经常写程序,读别人的程序来提高VC能力。
要应付找工作,建议还是突击C/C++,因为大部分公司都是考察基本的C语言掌握程度。推荐一本必备的参考书:林锐博士的《高质量C++编程指南》会对你复习考试非常有帮助:)
祝好运!
2004-11-24 11:47 | 水精灵      

#3楼    回复  引用    

// http://www.cnblogs.com/waterspirit/articles/65981.html

/*
Constructors cannot return values, even if they have return
statements. Specifying a constructor with a return type is an error,
as is taking the address of a constructor.

构造函数不能够返回值,即使它们有return语句.指定一个构造函数的返回
类型或取它的地址都是错误的.
*/

class A {
public:
A();
};
A::A() {
return; // ok
//! return void; // error
//! return 0; // error
}

int main() {
A a;
&A(); //ic++下: warning #1563: taking the address of a temporary
//devC++下: [Warning] taking address of temporary

return 0;
}

"taking the address of a constructor"
我觉得take the address 应该是"取地址"的意思吧??
2005-09-01 00:15 | java牌咖啡 [未注册用户]

#4楼    回复  引用    

// 不好意思,上面那个有点不对,这个应该正确了,^_^
class A {
public:
A();
void f() { }
};
A::A() {
return; // ok
//! return void; // error
//! return 0; // error
}

void main() {
A a;
//! &A::A; // error: a constructor or destructor may not have its
// address taken

&A::f; // ok
}
2005-09-01 00:23 | java牌咖啡 [未注册用户]

#5楼    回复  引用    

&A(); //ic++下: warning #1563: taking the address of a temporary
// A() 生成一个对象,然后取它的地址.这跟取构造函数的地址根本是2回事,编译器都
// 已经说了.

sorry
2005-09-01 00:32 | java牌咖啡 [未注册用户]

#6楼    回复  引用    

什么情况下需要对类定义自拷贝函数和重载赋值函数?
答:"当成员变量不允许bitwise的情况下,需要 memberwis。比如string, list, vetor等。 "

说的不大明白. "成员变量不允许bitwise"??.
我的理解是:
成员变量按bitwise初始化是c++早期行为(很久很久以前).现在默认的拷贝初始
化函数(就是编译器默认提供的那个)是按成员初始化(memberwise).当仅仅是
memberwis时,会导致程序不正确的时候,例如类里含有指针成员变量等,这个时
候需要初始化.
只有这个类的对象不需要传递,返回或用某个已初始化的对象去初始化建立一个
新的对象,就没有必要定义拷贝构造函数.

而拷贝构造函数和operator=()这2个函数,只要有一个需要自已定义,一般情况下,
另一个也常常需要自己定义

楼主看的是哪本书??
2005-09-01 01:09 | java牌咖啡 [未注册用户]

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-02-04 11:42 编辑过


相关链接: