作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我搜索了各种网站,看到了很多程序,但找不到一个程序在做。这里是教程点的一个例子
#include <iostream>
using namespace std;
class Line {
public:
void setLength( double len );
~setLength(); <----- An error
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;
}
Line::~setLenght() //I tried void Line::~setLength too
{
cout<<"The function is deleted:"
}
double Line::getLength( void ) {
return length;
}
// Main function for the program
int main() {
Line line;
// set line length
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
我自己试过了,但我没有工作可能是我写的代码不是那么好,但我想知道是否有一个选项来解构参数化构造函数并将 void 声明为参数(例如:line::line(void)
) 使其成为参数化构造函数。
最佳答案
有几点可以减轻您的困惑:
setLength
不是构造函数,它只是 Line
类的一个方法。~ClassName
。void
声明一个函数作为单个参数是一种传统的 C 声明函数的无参数方式,在 C++ 中实际上并不需要。此外,它并不是真正特定于析构函数。关于c++ - 你能解构一个参数化的构造函数吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747312/
我是一名优秀的程序员,十分优秀!