ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I recently started programming in Java again. When in uni I learned what a constructor was but have to admit I never really understood it. Can anybody explain what a constructor is(in simple terms) and tell me how to know when to use one.
Anytime you want to create a new instance of your class (an object) the constructor will be automaticaly called to initialize the variables used or to allocate some memory. It's basicaly a prototype for classes.
I never took computer science, perhaps thats not technicaly correct, but thats the jist of it.. There's a good definition on that url, but heres a better example from a tutorial I have:
// example: class constructor
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle (int,int);
int area () {return (width*height);}
};
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
DrStoney hit it right on. the only thing i would change is to say is that the constructor is not "automatically" called, you have to explicitly call a constructor to create an instance. just like was showed in the example, you call the two int constructor and it creates a rect of given size.
A good way to think of "an object," in any language, is that it is a chunk of memory which has procedural code magically associated with it. That is, conceptually, how such things are implemented.
So, when we want our language to "create an object," what it actually does is to carve out a new block of memory, set up the magical pointers and stuff which allows that block of memory to "be an object," and then ... well, the object might have various needs of its own for setting things to initial values, initializing other objects that it might need, and so on. So, that's what a "constructor" does. It's an initialization routine. After allocating the memory and doing the magic, each of the constructors are run.
Likewise, when an object is being destroyed, "what the object might have initialized in its constructor, now needs to be dismantled." That's what a "destructor" is for: it's a termination or cleanup routine. So, if (for example) the constructor allocated a few other objects that it needed, then its destructor should dispose of those things. Once the destructor(s) associated with an object have finished executing, the object itself goes away.
Hmm... ok I'll bite.. so when your using an object, you are "constructing" it? I said automaticaly because I figure passing the variables to the object is done with out creating the Classname::Classname bit again... The terminology of this stuff is harder then doing it! Heh
the object must be constructed before it can be used,...
The whole point of constructors is that you cannot use the object until you have successfully constructed it.
The comp sci terminology is "invariant":
Quote:
A class describes a collection of encapsulated instance variables and methods (functions), possibly with implementation of those types together with a constructor function that can be used to create objects of the class...
Quote:
A class also describes a set of invariants that are preserved by every method in the class. An invariant is a constraint on the state of an object that should be satisfied by every object of the class...
The main purpose of a constructor is to establish the invariant of the class, failing if the invariant isn't valid. The main purpose of a destructor is to destroy the identity of the object, invalidating any references in the process...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.