Creating classes for other classes in C ++

If I have two classes, for example, as follows:

class A {...} class B {...} 

If I want to make class A public for class B , just make the members of class A publicly available, or can I just use public class A {...} ?

Is there a way to tell class B , for example, that only class A open to you? In other words, can I make public classes A private or private for others? Or is it just a matter of getting the class (inheritance)?

Thanks.

+7
source share
3 answers

There is a significant difference between how to publish a class and publish its contents.

If you define your class in the included file (.h file), you make your class public. Every other source file that includes this include file will know about this class and may, for example, have a pointer to it.

The only way to make the class private is to put its definition in the source file (.cpp).

Even when you publish a class, you do not have to make the contents of your class public. The following example is extreme:

 class MyClass { private: MyClass(); ~MyClass(); void setValue(int i); int getValue() const; }; 

If this definition is placed in an included file, each other source can reference (have a pointer to) this class, but since all methods in the class are private, no other source can construct, destroy, set its value or get its value.

You publish the contents of the public class by putting methods from it into the "public" part of the class definition, for example:

 class MyClass { public: MyClass(); ~MyClass(); int getValue() const; private: void setValue(int i); }; 

Now everyone can create and destroy instances of this class and even get the value. However, setting a value is not publicly available, so no one can set a value (except for the class itself).

If you want to make the class publicly available only for another class of your application, but not for the full application, you must declare that the other class is different, for example:

 class SomeOtherClass; class MyClass { friend SomeOtherClass; public: MyClass(); ~MyClass(); int getValue() const; private: void setValue(int i); }; 

Now SomeOtherClass can access all private methods from MyClass, so it can call setValue to set the value of MyClass. All other classes are still limited by public methods.

Unfortunately, in C ++ there is no way to make only part of your class public for a limited set of other classes. So, if you make another class a friend, it will be able to access all the private methods. Therefore, limit the number of friends.

+15
source

You can use friendship.

 class A { friend class B; private: int x; }; class B { B() { A a; ax = 0; // legal }; 
+3
source

If B has a strong interdependence with A, I suggest you use a nested class. Fortunately, a nested class can be protected or closed.

 class A { protected: // the class A::B is visible from A and its // inherited classes, but not to others, just // like a protected member. class B { public: int yay_another_public_member(); }; public: int yay_a_public_member(); }; 
+3
source

All Articles