简体   繁体   中英

Accessing private member variables of a class from a static method

I am able to access the private member variable of the class shown in below code directly using an object instance (pointer to object). As per my understanding private members should not be accessible. Can someone please help to explain the reason behind this behaviour?

#include <iostream>

class myClass;
using myClassPtr = std::shared_ptr<myClass>;

class myClass {
public:
    myClass(unsigned int val) : m_val(val) {}
    ~myClass() {}

    static
    myClassPtr create(unsigned int val) {
        myClassPtr objPtr = nullptr;
        objPtr = std::make_shared<myClass>(val);
        if(objPtr) {
            std::cout << objPtr->m_val << std::endl;
        }
        return objPtr;
    }
private:
    unsigned int m_val;
};

int main () {
    myClassPtr objPtr = myClass::create(10);
    return 0;
}

Output

anandkrishnr@anandkrishnr-mbp cpp % g++ static_test.cc -std=c++11 -Wall -Werror
anandkrishnr@anandkrishnr-mbp cpp % ./a.out
10
static myClassPtr create(unsigned int val) {

create() is a static method of myClass , it is a member of this class. As such it it entitled to access all private members and methods of its class. This right extends not only to its own class instance, but any instance of this class.

As per my understanding private members should not be accessible.

... except by members of their class.

Let's create a completely pointless copy constructor for your class, the same copy constructor you would get by default:

myClass(const myClass &o) : m_val{o.m_val} {}

This copy constructor has no issues, whatsoever, of accessing m_val of the passed-in object. Exactly the same thing happens here. m_val is a private member of its class. It doesn't mean that only an instance of the same exact object can access its private members, it means that any instance of the class, or a static class method, can access the private class members.

The nature of access modifiers on a class is to restrict the use of a class's members outside of that class.

Since create() is inside myClass , create() is allowed to access any private members (or really any members) of myclass that you have declared. including m_val

For example, lets make a simple class,

if you had the code:

class box {
private:
    int width;
    int height;
};
int main() {
    box b;
    b.width;

    return 0;
}

The compiler would throw the error: error C2248: 'box::width': cannot access private member declared in class 'box'

Because the main() function is declared outside of the class, therefore, the main() function is not allowed to access private members of the class box .

If we wanted to access, say the width, outside of that class, we would need to then make those members public to allow everything else in the program to see those members:

class box {
private:
    int width;
    int height;
public:
    int pubWidth;
    int pubHeight;
};
int main() {
    box b;
    b.pubWidth;

    return 0;
}

This time, the members we are trying to access are public, ( pubWidth ) allowing outside functions, classes, etc. from accessing these members of box

Programiz in my opinion, has a great, easy to understand article about access modifiers in c++. I would reccommend giving it a look. link here .

Little note: If not specified, all members in a class are private with the exception of constructors, destructors, and maybe some special use-cases I can't think of off the top of my head.

hope this clears it up!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM