1

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.

Ball Rall
  • 63
  • 8
  • Can someone tell me what this warning means "The question you're asking appears subjective and is likely to be closed". I got it before posting the question – Ball Rall Jun 25 '19 at 05:25
  • 4
    What do you mean by "deconstruct" a parameterized constructor? – Mat Jun 25 '19 at 05:27
  • 3
    a destructor will destruct the object created, doesn't matter if it was created from a parameterized constructor or not. if that helps – Arne Jun 25 '19 at 05:31
  • @Mat in the code given i have line function and ~line (a destructor) but when i try ~getLenght or getLength it does not work and it either say a destructors does not take parameters or it's declaration is invalid – Ball Rall Jun 25 '19 at 05:37
  • @ArneSim Ok thanks so can i know how to destruct the setlenght function like said in my previous comment i used to write or declare a destructor for it in many ways but none work. – Ball Rall Jun 25 '19 at 05:39
  • How about showing us your attempt that actually doesn't work instead of code from some other site that doesn't have the issue? Present your [mcve] please. – StoryTeller - Unslander Monica Jun 25 '19 at 05:40
  • @StoryTeller Ok i will update the code – Ball Rall Jun 25 '19 at 05:41
  • 1
    @BallRall: you don't destruct functions. You destruct objects. Functions just live forever. (Ignoring dynamic linking.) – Mat Jun 25 '19 at 05:43
  • Your title doesn't make sense to me. Can you please clarify? –  Jun 25 '19 at 05:43
  • 2
    Regarding *The question you're asking appears subjective and is likely to be closed*, normally it's a warning that your question requires too much guesswork or opinion to produce a good answer. In this case I think it's a false positive, the heuristic the spits out those warnings got confused and made a bad call. – user4581301 Jun 25 '19 at 05:44
  • @StoryTeller my i changed the code basically i added the part that causes the error and marked it with <----- – Ball Rall Jun 25 '19 at 05:47
  • @Chipster sorry chipster i am using stack for a short time so did not know how to ask a really good question and i asked it this way but i meant to say whether i can deconstruct the function getLength or setLength since it was giving me errors and is taking void in () "Eg:: Line::Line(void)<_---------- is this considered as a parameterized constructor. – Ball Rall Jun 25 '19 at 05:51
  • Sorry guys looks like i was confused r3mus n0x helped me a lot thanks and sorry for wasting your times – Ball Rall Jun 25 '19 at 05:53
  • @BallRall Ah, I got you now. Well, the short answer is no For the long answer, see the answer r3 gave. –  Jun 25 '19 at 05:53

1 Answers1

2

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.
r3mus n0x
  • 5,954
  • 1
  • 13
  • 34
  • In fact, if it violates [the rule of 0/3/5](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three), you actually shouldn't! –  Jun 25 '19 at 05:49
  • @r3mus n0x OK thanks that did solve my problem,I was confused.Thanks for helping out – Ball Rall Jun 25 '19 at 05:54