|
|
|
|
|
by steveklabnik
922 days ago
|
|
Wikipedia defines it as > a constructor is a special type of function called to create an object. Now, I don't think Wikipedia is always the authority on everything, but this is how I view them as well. A constructor is kinda like a callback that the language runtime invokes when an something is created. For example, in C++: class Foo {
public:
Foo() {
// your code goes here
}
};
int main() {
Foo a; // constructor invoked here
}
(please excuse formatting, there is no unified C++ style so I just picked one)or in Ruby: class Foo
def initialize
# your code goes here
end
end
Foo.new # constructor invoked here
These "constructors" in Rust don't work like that at all. They are not special member functions that are invoked when something is created; they are the syntax to create something. |
|