I am working with a numerical library deal.ii, within which a lot of numerical tools are integrated. What I found weird is that I can call member functions directly without defining an object. For example, I can directly call
Vectortools::interpolate_boundary_condition();
Could you tell me when I can directly call the member functions without defining an object? Thank you!
Because a member function is meaningless without an object to invoke it on, you can't do this directly (if The X Window System was rewritten in C++, it would probably pass references to objects around, not just pointers to functions; naturally the objects would embody the required function and probably a whole lot more ...
Only static class functions can be called without an object using the Class::function() syntax. So, you should add static as a keyword to the definition of your functions inside the Cat class.
Static methods are the methods in Java that can be called without creating an object of class.
You could declare the function outside the vector class but in the same namespace/file and then define it accordingly. And then in the cpp: namespace math { Vector::Vector() { ... } double scalar(const Vector& v1, const Vector& v2) { ... } }
There's two cases you can do this:
The member function is declared static
-- in that case it is basically a free function but scoped to the class. Notably, this
cannot be used in a static function since an object is not required to invoke it.
When you are within a member function whose this
pointer is implicitly convertible to a pointer to the type being invoked on (Vectortools
in this case). Note that this is invoked on an object (implicitly *this
). This can be used to invoke an inherited member that is being overridden or hidden:
class A {
public:
virtual void foo();
};
class B : public A {
public:
virtual void foo() override;
};
void B::foo() {
// Do something
// Invoke the method we've overidden from A.
A::foo();
// Then do something else
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With