Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can changing an access specifier in a program change the behaviour of the program?

Tags:

c++

c++11

I wonder is there any cases in a C++ program wherein changing an access specifier (public/protected/private) in the code lead to a change in the behaviour of that program?

like image 286
Gupta Avatar asked Jan 27 '23 12:01

Gupta


1 Answers

Templates allow you to do different things depending on whether a member or method is accesible or not. Just as a random example, consider this:

#include <type_traits>
#include <iostream>

struct foo_private {
    private:
        foo_private() {}
};
struct foo_public {
    public:
        foo_public() {}
};

int main() {        
    std::cout << std::is_default_constructible<foo_private>::value;
    std::cout << std::is_default_constructible<foo_public>::value;       
}
like image 88
463035818_is_not_a_number Avatar answered Jan 30 '23 12:01

463035818_is_not_a_number