Calling class function without an instance of it

Dec 29, 2013 at 9:38am
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?

I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>

class Amphibian
{
public:
    Amphibian(std::string amp_name);
    void checkMembers();

private:
    static int members;
    std::string name;
};

int Amphibian::members = 0;

Amphibian::Amphibian(std::string amp_name)
{
    name = amp_name;
    members++;
}

void Amphibian::checkMembers()
{
    std::cout << members << std::endl;
}

int main()
{
    Amphibian amp1("Frog");
    Amphibian amp2("Toad");

    std::cout << "Members in Amphibian family: ";

    amp2.checkMembers(); //I cannot use Amphibian.checkMembers(); It gives error.
    return 0;
}
Last edited on Dec 29, 2013 at 9:40am
Dec 29, 2013 at 9:52am
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

class Dog {
public:
    Dog();
    ~Dog();

    static unsigned int dogCount();

private:
    static unsigned int m_dogCount;
};

unsigned int Dog::m_dogCount = 0;

Dog::Dog(){
    Dog::m_dogCount++;
}

Dog::~Dog(){
    Dog::m_dogCount--;
}

unsigned int Dog::dogCount(){
    return m_dogCount;
}

int main()
{
    Dog *myDog = NULL;
    std::cout << "There are currently: " << Dog::dogCount() << " dogs." << std::endl;
    myDog = new Dog;
    std::cout << "There are now " << Dog::dogCount() << " dogs. " << std::endl;

    return 0;
}

Dec 29, 2013 at 10:00am
Thanks :D.
Also can you explain what is the 'protected: ' protection level? I understand public and private but not protected.
Last edited on Dec 29, 2013 at 10:04am
Dec 29, 2013 at 10:32am
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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();
}
Dec 29, 2013 at 10:38am
OK I get it now. Thanks :).
Topic archived. No new replies allowed.