Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Is private really private?

I was trying out the validity of private access specifier in C++. Here goes:

Interface:

// class_A.h

class A
{
public:
    void printX();
private:
    void actualPrintX();
    int x;
};

Implementation:

// class_A.cpp
void A::printX()
{
    actualPrintX();
}

void A::actualPrintX()
{
    std::cout << x:
}

I built this in to a static library (.a/.lib). We now have a class_A.h and classA.a (or classA.lib) pair. I edited class_A.h and removed the private: from it.

Now in another classTester.cpp:

#include "class_A.h"    // the newly edited header

int main()
{
    A a;

    a.x = 12;           // both G++ and VC++ allowed this!
    a.printX();         // allowed, as expected
    a.actualPrintX();   // allowed by G++, VC++ gave a unresolved linker error

    return 0;
}

I know that after tampering a library's header all bets are off (I mean, system integrity, etc.) Albeit the method being hacky, is this really allowed? Is there a way to block this? Or am I doing something wrong here?

like image 712
legends2k Avatar asked Feb 16 '10 13:02

legends2k


People also ask

Is private setting really private?

Private browsing does not make you anonymous online. Anyone who can see your internet traffic – your school or employer, your internet service provider, government agencies, people snooping on your public wireless connection – can see your browsing activity.

Is private safari really private?

When you use Private Browsing, the details of your browsing aren't saved, and the websites you visit aren't shared with your other devices. Safari won't remember the pages you visit, your search history, or your AutoFill information.

Is private Google private?

Private browser options Google Chrome's Incognito Mode was designed to make it easier to share computers at places like the office. But enabling Incognito Mode doesn't keep your identity private.

Can private browsing be tracked?

Incognito mode doesn't prevent web tracking Your incognito history can still be seen by your ISP, and the websites you visit can still track you. Incognito mode does not mean you're browsing anonymously.


2 Answers

private is not a security mechanism. It's a way of communicating intents and hiding information that other parts of your program do not need to know about, thus reducing overall complexity.

Having two different header files is not standards compliant, so technically you're entering undefined behaviour territory, but practically, as you've found, most compilers won't care.

like image 152
Pontus Gagge Avatar answered Sep 21 '22 14:09

Pontus Gagge


You've strayed beyond what's allowed in C++, so what you're doing isn't allowed - but of course it may work on some compilers in some situations.

Specifically, you're violating the One Definition Rule.

This article by Herb Sutter explains it quite nicely - it also provides a legal and portable way of circumventing the access specifier system.

like image 23
JoeG Avatar answered Sep 18 '22 14:09

JoeG