- 浏览: 435470 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (538)
- C/C++ Primer (69)
- Objective-C Primer (102)
- Python Primer (19)
- JavaScript Primer (1)
- Java Primer (37)
- PHP Primer (17)
- 泛 Linux (37)
- Shell Script (21)
- APUE (21)
- UNP__1&2 (19)
- NetWork (7)
- Oracle周边 (38)
- Mysql里边 (6)
- Windows技 (9)
- 简单算法 & 数据结构 (14)
- 设计模式 (6)
- GTK历程 (12)
- 工具使用 (25)
- 杂事 (23)
- 一些概念 (17)
- Web方面 (10)
- myCodeTools (9)
- ^未 竟$ (13)
- 硬件通信 (2)
- Games (1)
最新评论
当时学习敲的
14.3
这题还真带出问题来:临时对象的返回、参数对象自我保护的const机制。
原书的拷贝函数是没带const的,X::X(X& x) => X::X(const X& x)。
引用是很好的东西,要好好保护它,故聘请保镖const。
const还能与非const重载的……
14.2
对拷贝函数,并不用那么在意,其实就是堆上的数据一共有几份。
14.1
此题我主要是想体会指针对象数组,与Java的稍有不同
14.3
这题还真带出问题来:临时对象的返回、参数对象自我保护的const机制。
原书的拷贝函数是没带const的,X::X(X& x) => X::X(const X& x)。
引用是很好的东西,要好好保护它,故聘请保镖const。
const还能与非const重载的……
#include <iostream> using namespace std; class X { protected: int y; public: X(int); X::X(const X&); // 原来的 X::X(X&); ~X(); }; X::X(int y) { cout << "构造:" << y << endl; this->y = y; } X::X(const X& x) // 原来的 X::X(X& x) { cout << "拷贝:" << this->y << endl; } X::~X() { cout << "析构:" << this->y << endl; } X fx(X x) // 这里隐藏了拷贝函数的调用,返回临时对象时,会调用拷贝函数 { // 而且参数会自我保护,提升为const return x; // 关于临时对象,钱能书:14.7节 P323 } int main(int argc, char *argv[]) { // X a(1); // X b = a; a = fx( X(2) ); return 0; }
14.2
对拷贝函数,并不用那么在意,其实就是堆上的数据一共有几份。
#include <iostream> using namespace std; class Vector { protected: int size; int* buffer; public: Vector(int s = 100); Vector::Vector(Vector& v); ~Vector(); int& Elem(int ndx); void Display(); void Set(); }; Vector::Vector(int s) // 所谓的拷贝函数 { size = s; buffer = new int[size]; for (int i = 0; i < size; i++) { buffer[i] = i*i; } } Vector::Vector(Vector& v) { size = v.size; buffer = new int[size]; for (int i = 0; i < size; i++) { buffer[i] = i*i; } } Vector::~Vector() { delete[]buffer; } int& Vector::Elem(int ndx) { if (ndx < 0 || ndx >= size) { cout << "error in index" << endl; exit(1); } return buffer[ndx]; } void Vector::Display() { for (int i = 0; i < size; i++) { cout << buffer[i] << endl; } } void Vector::Set() { for (int i = 0; i < size; i++) { buffer[i] = i + 1; } } int main(int argc, char *argv[]) { Vector a(5); a.Display(); cout << endl; Vector b = a; a.Set(); b.Display(); return 0; }
14.1
此题我主要是想体会指针对象数组,与Java的稍有不同
#include <iostream> using namespace std; class Samp { protected: int i; int j; public: Samp() { cout << "Constructing:" << i << endl; } ~Samp() { cout << "Destroying:" << i << endl; } public: void Setij(int a, int b){i = a; j = b;} int GetMulti(){return i*j;} }; int main(int argc, char *argv[]) { Samp* p = NULL; p = new Samp[10]; // Samp[] p = new Samp[10]; // Java的做法 if (!p) { cout << "Allocation error!" << endl; return -1; } for (int i = 0; i < 10; i++) { p[i].Setij(i, i); } for (int j = 0; j < 10; j++) { cout << "Multi[" << j << "]is:" << p[j].GetMulti() << endl; } delete[]p; // 忘了不就很危险~ cout << "Hello, world" << endl; return 0; }
发表评论
-
float equal
2013-05-23 18:21 770- (BOOL)floatA:(float)f1 equalB ... -
C++序列化
2012-07-12 09:51 528http://hi.baidu.com/ewook/item/ ... -
C++著名内裤
2011-12-02 14:00 7641、C++各大有名库的介绍 ... -
C++写C
2011-09-06 17:23 579在C++中写C,标准头文件写法 #ifndef __AB ... -
errno.h
2011-05-05 14:27 644#define EPERM 1 ... -
__cplusplus
2011-04-21 15:09 698http://www.cnblogs.com/stonecra ... -
Pthread__常用
2011-04-21 11:46 804http://blogold.chinaunix.net/u/ ... -
常用预编译
2011-04-21 10:32 601#if defined(WIN32) || defined ... -
C++__容器
2011-04-18 10:16 512http://apps.hi.baidu.com/share/ ... -
C++模板
2011-03-29 14:27 590内容主要来至《钱能C++程序设计教程》&《C++ Pr ... -
交叉引用、前置声明
2011-03-02 09:40 762有两个类相互引用了,编译器报错。 原因是两个类交叉引用: A ... -
typedef的四个用途和两个陷阱
2010-12-14 12:34 602http://hi.baidu.com/changfeng01 ... -
第五届开源小组招新题目
2010-12-03 21:56 693这是学校的开源小组招新题。 08年时我是第二届的组长,不知不觉 ... -
string const char*
2010-12-02 16:13 726http://topic.csdn.net/u/2008092 ... -
CPP Container
2010-11-10 09:20 641延后整理。。。 -
C语言运算符优先级
2010-11-02 17:45 730优先级 运算符 ... -
point point
2010-11-02 17:23 574http://learn.akae.cn/media/ch23 ... -
位运算
2010-11-01 11:33 741千百年来,位运算始终没出现在我写的代码里,今天终于出现了。 ... -
Unix环境下操作特大文件
2010-11-01 11:03 561http://hi.baidu.com/jiangfeng11 ... -
GCC在C语言中内嵌汇编
2010-10-22 14:48 1326http://hi.baidu.com/liu_bin0101 ...
相关推荐
绝对第二版,使C++技术更上一层楼,快快下载吧!
钱能-c++程序设计教程习题答案,没记错的话是第三版的,记得好像有人说没找到
钱能的c++程序设计教程,包含有课后习题答案、教学PPT、书上的程序题。
钱能C++程序设计教程(第二版)课件\C++程序设计教程2.ppt
钱能C++程序设计教程课后习题答案【第2版】.rar
钱能C++程序设计教程(第二版)课件\C++程序设计教程10.ppt
钱能c++程序设计教程习题答案 可供学习c++的朋友学习参考
钱能C++程序设计教程(第二版)部分答案,绝对真实,是第二版的。只不过只是一部分的答案。这不像有些人,明明发的是第一版的,却硬要说是第二版的。
钱能C++程序设计教程(第二版)课件\C++程序设计教程15.ppt ~~~~ ~~~~~~~~~~~~~~~~~完~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
钱能C++程序设计教程(第二版)课件\C++程序设计教程14.ppt
钱能C++程序设计教程第九章课后习题所有作业
钱能C++程序设计教程(第二版)ppt课件,源代码,实验指导,一应俱全。
很好 这是很好的答案 尽管不是很全 但是每章都有答案 是根据难易程度写的答案
钱能先生精典著作PDF版.
清华钱能C++程序设计教程第八章第六题代码,课后习题的答案啊
钱能C++程序设计教程(第二版)课件\C++程序设计教程11.ppt