如何使用带参数的对象作为类成员
2008-02-25 16:58
369 查看
看如下示例:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CStudent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent(int age = 16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
int m_age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
lpublic:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2(10); // 试图声明一个m_age = 10的成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 错误error C2059: syntax error:’constant’
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如上例,要想在类CClass中使用一个带参数的对象m_student2,以上的定义方式是错误的。
语法上说,编译器把这个CStudent m_student2(10);当成了函数声明,报告错误:error C2059: syntax error:’constant’
从类定义的角度来看,定义成员变量时并不会实例化该变量,没有内存分配,但是CStudent m_student2(10);这种写法也意味着实例化一个对象,所以是不允许的。那么,这也意味着直接声明一个带参数的成员变量是不可能的,不知道这种理解对不对?
现在的解决的办法:
1. 定义一个CStudent的指针,在CClass的构造函数中,定义student2,在构造函数中实例化是没有问题的。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_pStudent2 = new CStudent(10); // 实例化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~CClass()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete m_pStudent2; // ??不知道这里是不是必须delete
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 如果不写CClass对象失效后 会自动释放吗?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_pStudent2; //定义指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2. 在CStudent中加入SetAge()方法,CClass中定义CClass m_student2,然后在CClass的构造函数中调用m_student2.SetAge(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CStudent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent(int age = 16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetAge(int age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
int m_age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_student2.SetAge(10); //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2; //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
不知道是否有更好的解决方法,希望高手指点~~
新求的一中写法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass():m_student2(10); // 参数赋值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
感觉是最合适的写法了。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CStudent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent(int age = 16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
int m_age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
lpublic:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2(10); // 试图声明一个m_age = 10的成员
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 错误error C2059: syntax error:’constant’
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
如上例,要想在类CClass中使用一个带参数的对象m_student2,以上的定义方式是错误的。
语法上说,编译器把这个CStudent m_student2(10);当成了函数声明,报告错误:error C2059: syntax error:’constant’
从类定义的角度来看,定义成员变量时并不会实例化该变量,没有内存分配,但是CStudent m_student2(10);这种写法也意味着实例化一个对象,所以是不允许的。那么,这也意味着直接声明一个带参数的成员变量是不可能的,不知道这种理解对不对?
现在的解决的办法:
1. 定义一个CStudent的指针,在CClass的构造函数中,定义student2,在构造函数中实例化是没有问题的。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_pStudent2 = new CStudent(10); // 实例化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
~CClass()...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
delete m_pStudent2; // ??不知道这里是不是必须delete
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 如果不写CClass对象失效后 会自动释放吗?
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_pStudent2; //定义指针
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2. 在CStudent中加入SetAge()方法,CClass中定义CClass m_student2,然后在CClass的构造函数中调用m_student2.SetAge(10);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CStudent
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent(int age = 16)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
void SetAge(int age)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_age = age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
private:
int m_age;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
m_student2.SetAge(10); //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2; //
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
不知道是否有更好的解决方法,希望高手指点~~
新求的一中写法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class CClass
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CClass():m_student2(10); // 参数赋值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
CStudent m_student2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
感觉是最合适的写法了。
相关文章推荐
- WebService学习之如何使用实体对象作为参数
- Java学习笔记43. 使用对象作为方法的参数
- 如何使用setObject为PreparedStatement对象传入动态参数?
- JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
- servlet(三)-如何使用传参-三种转发方式、传递参数以及对象中数据的范围
- 什么是引用,如何使用引用?使用引用作为函数参数,相关例题
- 如何去使用对象中的成员
- 【PHP面向对象(OOP)编程入门教程】如何去使用对象中的成员
- JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
- JMeter 如何把上一个请求的结果作为下一个请求的参数 —— 使用正则提取器
- 在 Angularjs 中 ui-sref 和 $state.go 如何传递单个多个参数和将对象作为参数
- springMVC中使用List和Map绑定对象作为参数的使用方法
- error C2648: “MyDoublyLinkedlist<int>::length”: 将成员作为默认参数使用要求静态成员
- 类对象及类成员函数作为函数参数的方法
- jmeter如何将上一个请求的结果作为下一个请求的参数——使用正则提取器
- 反射获取方法名及参数名,反射获取参数作为对象使用
- 将对象作为参数传递给成员函数
- 66.6 设计一个雇员类 employee,存储雇员的姓名、编号和生日等信息,要求该类使用 上一题设计的日期类作为成员对象。雇员类的使用如下: //定义一个雇员,其雇员号为 10,生日为 1970 年
- linux多线程创建时使用类成员函数作为参数
- 如何正确使用指针(指针作为输出型参数)与结构体指针作为函数参数