返回对象与应用区别:

拷贝构造器发生的时机:

1.构造新对象 A a, A b = a;

2.传参或返回对象

对于普通变量来说,传引用效果不是很明显,对于类对象而言,传对象效果很高。

传引用等价于扩大了原对象的作用域。

#include<iostream>
using namespace std;
class A
{
public:
A()//无参构造器
{
cout<<this<<" constructor"<<endl;
}
~A()//析构器
{
cout<<this<<" destructor"<<endl;
}
A(const A & another)//拷贝构造器
{
cout<<this<<" cpy constructor from"<<&another<<endl;
}
A & operator=(const A & another)//运算符重载
{
cout<<this<<" operator"<<&another<<endl;
}
}; void func(A a)
{ }
void func1(A &a)
{ } int main()
{
A x;//调用构造器
A y = x;//拷贝构造器
func(x);//调用拷贝构造器
/*
0x61fe9e constructor
0x61fe9d cpy constructor from0x61fe9e
0x61fe9f cpy constructor from0x61fe9e
0x61fe9f destructor
0x61fe9d destructor
0x61fe9e destructor
有构造器就有析构器
*/ func1(x);//没有调用任何构造器
/*
0x61fe9e constructor
0x61fe9d cpy constructor from0x61fe9e
0x61fe9f cpy constructor from0x61fe9e
0x61fe9f destructor
0x61fe9d destructor
0x61fe9e destructor
*/ return 0;
}

栈上的对象是可以返回的,但不能返回栈上的引用(除非返回对象本身)。

#include<iostream>
using namespace std;
class A
{
public:
A()//无参构造器
{
cout<<this<<" constructor"<<endl;
}
~A()//析构器
{
cout<<this<<" destructor"<<endl;
}
A(const A & another)//拷贝构造器
{
cout<<this<<" cpy constructor from"<<&another<<endl;
}
A & operator=(const A & another)
{
cout<<this<<" operator"<<&another<<endl;
}
}; A func(A &a)
{
return a;
} int main()
{
A x;//调用构造器
func(x);//发生了一次拷贝构造,但上述没有返回值的时候是没有任何构造器 
/*
0x61feae constructor
0x61feaf cpy constructor from0x61feae
0x61feaf destructor
0x61feae destructor
*/
return 0;
}
int main()
{
A x;//调用构造器
A t;//调用构造器
t = func1(x);//x作为参数传入并没有调用构造器,而是返回大时候调用了拷贝构造器,(func1函数返回的值拷贝到临时变量中,再从临时变量拷贝到t,用来运算符重载)
cout<<"&t "<<&t<<endl;
/*
0x61fe9e constructor
0x61fe9d constructor
0x61fe9f cpy constructor from0x61fe9e
0x61fe9d operator0x61fe9f
0x61fe9f destructor
&t 0x61fe9d
0x61fe9d destructor
0x61fe9e destructor
*/
return 0;
}
A func()
{
A b;
return b;
}
int main()
{
A t;
t = func();
cout<<"&t "<<&t<<endl; /*
0x61fe9e constructor
0x61fe9f constructor
0x61fe9e operator0x61fe9f
0x61fe9f destructor
&t 0x61fe9e
0x61fe9e destructor
*/ return 0;
}
A& func()
{
A b;
return this;//是可以的,因为本身对象还没有销毁,如果返回的是b,返回后func销毁,有时候会正确,有时候会错误。
}

string类字符拼接的实现

mystring mystring::operator+(const mystring & another)
{
mystring tmp;//
delete []tem._str;
int len = strlen(this->_str);
len += strlen(another._str);
tmp._str = new char[len+1]; memset(tem._str,0,len+1);//必须要的
strcat(tem._str,this->_str);
strcat(tem._str,another._str);
return tmp;
}

c与c++关于字符串的处理对比

c基于字符数组,利用一些方法strcpy,strlen,strcat等操作。

c++所有操作围绕字符指针。通过此指针进行操作,整体设计思想是面向对象思想。

面向对象的思想练习

#include<iostream>
#include<unistd.h>
#include<iomanip>
#include<time.h> using namespace std;
class Clock
{
private:
int hour;
int min;
int sec;
public:
Clock()
{
time_t t = time(NULL);
struct tm ti = *localtime(&t);
hour = ti.tm_hour;
min = ti.tm_min;
sec = ti.tm_sec;
}
void run()
{
while(1)
{
show();//显示函数
tick();//数据跟新函数
}
}
private:
void show()
{
system("cls");//windows下的清屏cls
cout<<setw(2)<<setfill('0')<<hour<<":";
cout<<setw(2)<<setfill('0')<<min<<":";
cout<<setw(2)<<setfill('0')<<sec;
}
void tick()
{
sleep(1);
if(++sec == 60)
{
sec = 0;
min += 1;
if(++min == 60)
{
min = 0;
hour += 1;
if(++hour == 24)
{
hour = 0;
}
}
}
}
} int main()
{
Clock c;
c.run(); return 0;
}

类成员的存储

#include<iostream>
using namespace std;
class Time
{
private:
int hour;
int min;
int sec;
public:
Time(int h,int m,int s)
:hour(h),min(m),sec(s)
{ }
void display()
{
cout<<hour<<min<<sec<<endl;
//<=>
//cout<<this->hour<<this->min<<this->sec<<endl;
}
/*
void display(Time *t)
{
cout<<t->hour<<t->min<<t->sec<<endl;//自己通过显式传参。
}
*/ };
int main()
{
Time t(1,2,3),t1(2,3,4),t2(4,5,6);
cout<<sizeof(Time)<<"----"<<sizeof(t)<<endl;
t.display();
t1.display();
t2.display(); return 0;
}

上述代码display()函数,三个对象公用。对象在调用的时候传递了自己对象this。

对象拥有自己的存储空间,函数部分代码是公用的。表现的形式是每个对象在调用的时候传进自己的对象this。

注意事项

1,不论成员函数在类内定义还是在类外定义,成员函数的代码段都用同一种方式存储。

2,不要将成员函数的这种存储方式和 inline(内置)函数的概念混淆。inline 的逻辑意义是将函数内嵌到调用代码处,减少压栈与出栈的开支。

3,应当说明,常说的“某某对象的成员函数”,是从逻辑的角度而言的,而成员函数的存储方式,是从物理的角度而言的,二者是不矛盾的。类似于二维数组是逻辑概念,而物理存储是线性概念一样。

C/C++(C++返回对象与应用区别,类成员的存储)的更多相关文章

  1. PHP get_class 返回对象的类名

    get_class (PHP 4, PHP 5) get_class — 返回对象的类名 说明 string get_class ([ object $obj ] ) 返回对象实例 obj 所属类的名 ...

  2. c++中返回对象与返回引用的区别

    这几天在做用C++做课程设计,对其返回对象的实现感到迷惑. 通过对汇编代码的分析,可以清楚的看到,直接返回引用和返回对象的区别到底是什么. 分析的程序如下 #include<cstdio> ...

  3. Vue中data返回对象和返回值的区别

    速记:粗浅的理解是,事件的结果是影响单个组件还是多个组件.因为大部分组件是要共享的,但他们的data是私有的,所以每个组件都要return一个新的data对象 返回对象的时候 <!DOCTYPE ...

  4. JS中isPrototypeOf 和hasOwnProperty 的区别 ------- js使用in和hasOwnProperty获取对象属性的区别

    JS中isPrototypeOf 和hasOwnProperty 的区别 1.isPrototypeOf isPrototypeOf是用来判断指定对象object1是否存在于另一个对象object2的 ...

  5. Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别

    一.Java的反射机制   每个Java程序执行前都必须经过编译.加载.连接.和初始化这几个阶段,后三个阶段如下图:   其中

  6. js创建对象的三种方法:文本标识法和构造器函数法和返回对象的函数

    文本标识法和定义变量差不多,像这样 var obj = {name:'HanMM','2':'Dali'}; 函数构造器法  先创建一个对象函数 function Obj() { this.addre ...

  7. JS对象与Dom对象与jQuery对象之间的区别

    前言 通过问题看本质: 举例: js的写法:document.getElementById('save').disabled=true; 在jquery中我是这样写的 $("#save&qu ...

  8. 二.OC基础--1,对象的存储细节,2,#pragma mark指令,3,函数和对象方法的区别,4,对象和方法之间的关系 ,5.课堂习题

    1,对象的存储细节, 1. 当创建一个对象的时候:Person *p1 = [Person new],做了三件事情: 1,申请堆内存空间: 2,给实例变量初始化: 3,返回所申请空间的首地址; 2. ...

  9. js通过方法返回对象的注意点

    问题:js通过方法返回一个字面量对象和返回一个提前已经定义好的字面量对象有区别吗? 答案:有 我们先来看看第一种情况,fun1方法返回一个提前没定义的字面量对象,然后通过调用方法返回三个对象,分别是o ...

随机推荐

  1. [置顶] Netty学习总结(1)——Netty入门介绍

    1.Netty是什么? Netty是一个基于JAVA NIO类库的异步通信框架,它的架构特点是:异步非阻塞.基于事件驱动.高性能.高可靠性和高可定制性. 2.使用Netty能够做什么? 开发异步.非阻 ...

  2. 数组中出现一次的两个数(三个数)& 求最后一位bit为1

    对于两个数,对于结果中,剩余bit1来异或区分. 下面的解法,非常精简: int lastBitOf1(int number) { ); } void getTwoUnique(vector<i ...

  3. 安装xcode6 beta 后调试出现Unable to boot the iOS Simulator以及编译苹果官方Swift的demo报错failed with exit code 1的解决的方法

    苹果昨天公布新语言Swift(雨燕),须要安装xcode6 以及mac os 系统为10.9以上. (xcode6 beta 可在官方下载.须要登录开发人员账号:mac os 系统直接更新就可以.在此 ...

  4. mysql-幻读及其例子

    第一步建表并插入5条记录:  接下来我们看下大部分mysql所说的幻读现象: 事务1(开启事务查询发现没有记录6准备插入):  事务2(开启事务,发现没有记录6插入,并提交事务):  事务1:查询发现 ...

  5. 手机Android音视频採集与直播推送,实现单兵、移动监控类应用

    最新手机採集推送直播监控以及EasyDarwin开源流媒体平台的版本号及代码: EasyDarwin 开源流媒体云平台:https://github.com/easydarwin EasyClient ...

  6. nj09---util、inherits、inspect、events、error

    一.util全局变量 1.util.inherits(constructor,superConstructor) 此方法是一个实现对象间原型继承的函数.javaScript通过原型赋值来实现继承,细节 ...

  7. angularjs 模块化

    <!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...

  8. http --- 从输入URL到页面加载的过程发生了什么?

    可以分为这几个大的过程: DNS解析 TCP连接 客户端发送HTTP请求 服务器处理请求并返回HTTP报文 浏览器解析渲染页面 结束 其中(1)DNS解析可以理解为主寻找这个IP地址的过程,其中如果找 ...

  9. Python 从入门到精通 全程最佳实现梳理

    零零星星的时间,持续完善中...... 1.一些基础的必要信息归纳 Python 官网 www.python.org 发明者 吉多·范罗苏姆 发行时间 1991年,​26年前 编程泛型 多泛型.面向对 ...

  10. RecyclerView让列表嵌套如此简单

    平常开发时,相信像这样的页面,大家一定是遇到过的.这里比较坑爹的地方在于呢:列表嵌套.订单列表中的每一项,都包含一个商品列表.像这种需求,大家会如何实现呢? 这里呢,说一下我自己的思路,我没有使用列表 ...