您可以解构参数化的构造函数吗?
Can you deconstruct a parameterized constructor
我搜索了各种网站,看到了许多程序,但找不到单个程序。
#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 中实际上不需要。
相关文章:
- 没有合适的构造函数可以从"float"转换为"_D3DCOLORVALUE"
- 智能指针:没有合适的构造函数可以从 derived_object* 转换为 std::shared_ptr<abstract_base_object>
- std::vector 范围构造函数可以调用显式转换吗?
- 构造函数可以更改默认成员类型吗?
- std::vector 默认构造函数可以抛出异常吗?
- 为什么带有 const 关键字的构造函数可以工作,而没有它就不能工作?
- 如何初始化堆,以便静态构造函数可以在常规 MFC dll 中使用堆?
- 矩阵类编写构造函数,解构器和复制构造函数的方式
- move构造函数可以是隐式的吗
- 默认的默认构造函数可以抛出
- 没有合适的构造函数可以从 int 转换为 emp
- std::list 的默认构造函数可以<int>抛出吗?
- new[] 如果元素默认构造函数可以抛出
- 构造函数可以在 c++ 中调用另一个类的构造函数吗?
- C++ "没有合适的构造函数可以从<默认构造函数>转换为参数化构造函数
- 类模板,没有构造函数可以获取源(字符串)
- 没有合适的构造函数可以从"const char [8]"转换为"std::basic_string<wchar_t, std::char_traits<wchar_t>, std::
- c++中的派生类构造函数可以继承基类构造函数吗?
- c++中的委托构造函数可以在函数体中调用,也可以在初始化列表中调用
- 模板化构造函数可以代替已删除的副本构造函数吗