简体   繁体   中英

Can you deconstruct a parameterized constructor

I have searched various websites and saw many programs but could not found a single program doing it.Here are is an example from tutorials point

#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;
}

I have tried it myself but i did not work it may be that the code that I write was not that good but I wanted to know whether if there is an option to deconstruct a parameterized constructors and is declaring void as a parameter (Eg: line::line(void) ) making it a parameterized constructor.

A couple of points to alleviate your confusion:

  1. setLength is not a constructor, it's just a method of class Line .
  2. Methods don't have destructors, only classes do and they are always called ~ClassName .
  3. You don't actually need to provide the destructor if it doesn't do anything meaningful (like deallocating resources).
  4. Declaring a function with void as a single parameter is a legacy C way to declare a function without parameters and is not actually needed in C++. Also it is not really specific to destructors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM