Is there any point me adding a....

This is a discussion on Is there any point me adding a.... within the C++ Programming forums, part of the General Programming Boards category; For my class, I was ponderng adding a deconstructor. But would that just be plain redundant? I have no objects ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479

    Is there any point me adding a....

    For my class, I was ponderng adding a deconstructor. But would that just be plain redundant? I have no objects to destroy here, What do you think?

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    // create the class
    
    class Vehicle				// class name
    {
    public:
    	int passengers;			// number of passengers
    	int fuelcap;			// fuel capacity in gallons
    	int mpg;				// fuel used ( miles per gallon )
    
    	// this is a constructor for Vehicle
    
    	Vehicle ( int p, int f, int m );
    
    	int range();			// compute and return the range
    
    };							// end class vehicle
    
    // implement the Vehicle constructor
    
    Vehicle::Vehicle( int p, int f, int m )
    {
    	passengers = p;
    	fuelcap = f;
    	mpg = m;
    }
    
    // implement the range() member function
    
    int Vehicle::range()
    {
    	return mpg * fuelcap;
    }
    
    // main function
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	// pass values to Vehicle constructor
    
    	Vehicle miniVan ( 7, 16, 21 );	
    	Vehicle sportsCar ( 2, 14, 12 );	
    
    	int range;			// for first object
    	int range2;			// for second object
    
    	// compute the range assuming a full tank of gas
    	// for miniVan and sportsCar
    
    	range = miniVan.range();
    	range2 = sportsCar.range();
    
    	// output results to screen
    
    	cout << "A mini van can carry " << miniVan.passengers << " with a range of " << range << endl;
    	cout << "\nA sports car can carry " << sportsCar.passengers << " with a range of " << range2 << endl;
    
    	getchar();	// freeze output
    
    	return 0;	// indicates sucsessful termination
    }

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    If there's nothing to release on destroying the object, there's no use for a destructor.
    You could add one as a placeholder for future implementation, doing nothing right not, but the verdict is out on whether that's best practice or not.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    If you always have them, even when not strictly necessary, then it becomes a good habit.
    As such, you're not going to forget to add one when it does become necessary.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    deconstructor.
    It's called a d-e-s-t-r-u-c-t-o-r.

    I have no objects to destroy here
    Yes, you do:
    Code:
    Vehicle miniVan ( 7, 16, 21 );	
    Vehicle sportsCar ( 2, 14, 12 );
    All objects that are created have to be destroyed. However, the compiler supplies your class with a default destructor, and it calls the default destructor when your objects go out of scope. To see that in effect, create a destructor for your class that cout's a message to the screen, and you will see that it is called for all your objects.

    As you learn more about C++, you will learn about the various default constructors and destructors that the compiler supplies for your class.
    Last edited by 7stud; 01-30-2006 at 05:14 AM.

  5. #5
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    those objects have however no dynamically allocated memory so there's precious little for a destructor to do.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If there's precious little to do, then there's precious little code:
    Code:
    ~Vehicle() {}
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I would simply add three comments. One to say that the default destructor is sufficient. One to say that the default copy constructor is sufficient. And the third to say that the default copy assignment operator is sufficient. That way it tells someone looking at the class that you have handled those issues (by leaving the compiler's version to do the jobs). In the case of the copy constructor and assignment operator, it also prevents you from forgetting to add new properties to be copied.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. point to point
    By robarm4 in forum Game Programming
    Replies: 3
    Last Post: 05-02-2007, 12:08 PM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  4. Point passed rectangle...
    By Hunter2 in forum Game Programming
    Replies: 15
    Last Post: 10-10-2003, 10:57 AM
  5. observing whether a point is in a triangle???
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2003, 04:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21