这些对象都分配在栈上
//
// main.cpp
// 拷贝构造函数
//
// Created by Eric on 16/7/20.
// Copyright © 2016年 Eric. All rights reserved.
//
#include <iostream>
using namespace std;
class A{
public:
A(int a){
_a = a;
};
A(int a,int b){
_a = a;
};
A(const A& obj){
cout<<"A 拷贝构造函数调用\n";
}
int getA(){
return _a;
}
void setA(int a){
_a = a;
}
private:
int _a;
};
class Location
{
public:
/**
* 含参构造函数 构造函数初始化列表
*/
Location(int x = 0,int y = 0):a1(10){
_x = x;
_y = y;
_myP = (char *)malloc(100);
strcpy(_myP, "adfadaf");
cout<<"Constructor Object.\n";
}
Location(const Location &obj):a1(10){
cout<<"调用拷贝构造函数 \n";
}
/**
* 析构函数
*/
~Location(){
cout<<_x<<","<<"Object destroryed"<<endl;
if (_myP != NULL) {
free(_myP);
}
}
int getX(){
return _x;
}
private:
int _x,_y;
char *_myP;
A a1;
};
class B{
public:
B(int a,int n,int m,int a2n,int a2m):a1(n,m),a2(a2n,a2m){
_a = a;
}
A getA1(){
return a1;
}
A getA2(){
return a2;
}
void doThing(A &a){
a.setA(100);
printf("B 开始Do thing了\n");
}
private:
A a1;
A a2;
int _a;
};
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
B b1 = B(1, 2, 3, 4, 5);
A a1 = b1.getA1();
A a2 = b1.getA2();
printf("a1-->%d\n",a1.getA());
printf("a2-->%d\n",a2.getA());
b1.doThing(a1);
printf("a1-->%d\n",a1.getA());
return 0;
}