我想知道用Line().setLength(6.0);
创建对象之间的区别是什么?
和
Line line; line.setLength(6.0);
编译后结果是否相同?
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
// Member functions definitions including constructor
Line::Line(void) {
cout << "Object is being created" << endl;
}
Line::~Line(void) {
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len ) {
length = len;
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
int main( ) {
Line().setLength(6.0);
Line line;
line.setLength(6.0);
return 0;
}
答案 0 :(得分:0)
结果应始终相同。但是Line().setLength(6.0);
表示您将来无法更改该行的属性,而使用Line line; line.setLength(6.0);
可以访问变量line
,从而更改该行的属性。