Hi how can I access a class function like this: For example I have a class Foo with a public function getMembers(). How can I access it like this: Foo.getMembers(); without creating an instance of the class?
What you want is a static function. Since it is called without an instance of the class, it cannot be marked "const" or "volatile", and you don't have access to the "this" pointer. There are also static members, which are shared between all instances of the class. Heres an example of both
I assume you mean the access specifier, and not the type of inheritance- they're similar, but not identical. For now, don't worry about the type of inheritance, just use public inheritance.
A protected member is one which can be called by the class, OR by any class which derives from it. If you have class A which has a protected member, and class B which derives from A, both classes A and B can access the protected member.
#include <iostream>
class BaseClass {
protected:
void protectedFunction(){
std::cout << "protectedFunction called!" << std::endl;
}
};
class DerivedClass : public BaseClass {
public:
void dumbFunction(){
// This is valid, since this class derives from BaseClass
protectedFunction();
}
};
int main(){
DerivedClass dc;
// dumbFunction is public, so it is accessible anywhere
dc.dumbFunction();
// This is invalid, because protected members can't be called outside of the class,
// only by the class itself, and other classes which derive from it
dc.protectedFunction();
}