How to set methods private to module? - d

I have two D classes in one module. I would like class A to have a property, which can only be accessed from class A and class B. How would I do this?
class A {
int a = 5; // Make this accessible to, and only to, B.
}
class B {
this(in A pA) {
int b = pA.a;
}
}

private is private to a module, not a class. So, marking a symbol as private makes it so that only stuff in that module can access it.
package makes it so that only stuff in the same package can access the symbol.
protected makes it so that only stuff in that class and in classes derived from that class can access the symbol (unlike the others, protected makes no sense outside of classes).
public makes it so that anything can access the symbol.
private and package functions are never virtual, whereas protected and public functions are always virtual unless the compiler is able to determine that they don't need to be (which at this point, can pretty much only happen when the function is final and doesn't override a function in a base class).
So, as long as your two classes are in the same module, and their members are private, they can access each others members - as can anything else within the module - but nothing outside the module can access them. There is no way to restrict access within a module except for when the symbol is local to a function, so if you want to make it so that one class cannot access the members of another, then you're going to need to put them in separate modules.

just add private
the access modifiers are module based with protected as the only exception
http://dlang.org/attribute.html#ProtectionAttribute (emph mine)
Private means that only members of the enclosing class can access the member, or members and functions in the same module as the enclosing class. Private members cannot be overridden. Private module members are equivalent to static declarations in C programs.

Related

If the A class's member function is changed, client binary of the A class need to be rebuild?

1.A class is implemented in libA.so
class A
{
A_fun();
B_fun();
}
2.client use A class via libA.so
client{
use A_fun();
}
In this situation,
If A class's member function is added C_fun(),
class A
{
A_fun();
B_fun();
C_fun(); //added
}
As I know, If A_fun() parameter is changed, client have to rebuild.
But client is not use C_fun().
It this case, client binary need to be rebuild?
This is platform dependant. On most platforms adding a non-virtual member function to a class doesn't break the ABI. Generally you can make the following changes to a class without breaking the ABI:
Add non-virtual methods
Rename private methods and variables
Add static methods
Remove private methods
Remove private static methods
Add static member variables
Remove private static member variables

Which access specifiers will not affect a friend function?

Which access specifiers will not affect the friend function?
private and protected members of a class cannot be accessed from outside, private and protected member can be accessed anywhere or both
None of the access specifies affect a friend.
When you declare a function/method or class as a friend you are making it part of the public interface to your class (thus implying a tight cupping).
A friend is actually able to see all the members of your class (you could think of it as a member function but without the implied this parameter). As such it is actually tightly coupled to your class. Any change in the internal representation of your class is going to be reflect in changes to the implementation of all public interface members.

Need clarification on interface in OOP

With regards to a class, what does an interface mean? I think it refers to all the public functions of the class. Am I correct or does it mean something else?
I keep hearing it a lot but never quite noticed the explicit definition.
This one's the real question. What does it mean for a derived class to retain the interface of the base class it's derived from? I think it means that the public functions in the base class must be public in the derived class as well (which will be the case in public and protected inheritance). Am I mistaken?
Yes, the interface of a class is the collection of its visible member functions to the outside world, i.e. its public member functions. Some also include member variables in the interface, but it is not usually common to have public member variables (unless declared static). Most often, interfaces are implemented via abstract base classes. This is in contrast to Java, which has a different keyword for specifying interfaces.
Retaining the interface means having the public member functions in the base class visible across the class hierarchy. Also, you can override virtual functions so you get polymorphic behaviour, keeping the common interface. Note that only public inheritance preserves the interface, protected and private do not. Another way of failing to retain an interface is via name hiding in C++. Example: re-declaring Base::f(int) as Derived::f(float,float). In this case, the Base::f(int) is not longer visible in Derived, unless via a using Base::f; statement.

C++ access specifiers

I just want to make sure I got the idea of public and private right.
Regarding the private access specifier, does it mean:
Only accessed inside the class
Cannot be accessed from the object of the class unless there are public class methods that can be used to access them (Can other objects use those public functions?)
No other object can access them
And for public:
Accessed from the object of the class
Accessed from any other object
Is that right?
private : Only member functions and friends of class can access it.
public : Can be accessed anywhere where the object has scope.
Answering the questions -
private:
Yes
Yes. (Can other objects use those public functions? With out having class relations, one object of class cannot communicate to the other's members. )
Friends has access to private members of a class. So, answer depends upon your class has friends or not.
public:
Yes
Depends whether the object has hierarchical relations to the member's class you are trying to access.
I think there is an issue of vocabulary to begin with.
In C++ (and most languages) a class is a type. You can think about it as a blueprint to actually build something.
it describes the attributes that are held
it describes the methods to operate on those attributes
it describes the restrictions that apply: this is the "accessibility"
An object is produced by actually instantiating a class, that is, building what the blueprint described. It is more or a less a bundle of attributes. You can have several objects of the same class as you can have several houses from the same blueprint: note that their physical location is different for obvious reasons :)
Now, on to the accessibility. There are 3 typical levels of accessibility: public, protected and private.
public, as expected, means that everyone is given access to either attributes or methods
protected is somewhat less trivial. It means that only the object, or its children, may access the attributes (bad idea*) or methods. (Plus, in C++, friends)
private means that only the objects of that class (and not their children) may access the attributes or methods (Plus, in C++, friends)
Note: whatever the level of accessibility, an object has unrestricted access to all the attributes and methods of any object of the same class.
(*) Even though it pops up now and there, it is generally a bad idea to use protected attributes. The point of encapsulation is to hide the details, not only for the sake of it, but because by precisely controlling who can access the data, we can ensure that the class maintains its invariants (simple example, an array where you would store the size separately, you need to ensure that the "size" really represents the number of items in the array, at all times). Note: this restriction does not apply when you can seal a hierarchy, like in C# for example.
Private members can only be accessed by member functions and static functions of the same class and by friends of the class. It does not matter on which object that function is called. So the case
class Foo
{
private:
void bar() {}
public:
void baz(Foo& var)
{
var.bar();
}
}
is perfectly legal.
That seems correct. Data members and functions marked public can be accessed from anywhere by anyone. Data members and functions marked private can only be accessed by the class and its friends. However, a member function of a class can access data with any access specifier, so a public function can read and write private data members (this is used universally in OOP).
In c++ data and fn are encapsulated as 1 unit.
We begin a program by writing
preprocessor directives
Then, class declaration
Followed by function(fn) declaration where we also specify the access modifier ( public, private or protected)
& finally the main () program.
If we declare a fn
Private:the data within an object of a class is only accessed by fn defined within it- (the object which has the data and the private fn)
Public:the data can be accessed by any fn
Protected:similar to private however data can also be accessed by sub-classes that inherit the properties of another class.
Example if class A inherits from class B, thenA is a subclass of B.

Why defining private members below public members in C++?

In C++ sometimes in class definition public members are declared at first and privates later. But the variables or data members are normally private and used by the public methods. So, in this case variables are used but not even declared yet. Thus the code become difficult to understand. But I found renowned programmers, sites or books to declare the private members later. Does anybody knows what is the reason?
I do things that way round since users of my class don't care about the private members, they're interested in the public API (i.e. how to use my class).
Also, in header files I'm generally just declaring member functions, rather than defining them, so I'm not accessing any private members anyway.
We read text from top to bottom, so the most relevant information should be at the top. In a class definition, that's the public interface.
Private members and implementation should be hidden from the header file. Putting the private member definitions at the bottom is a quick way to do so. Perhaps it is better to use the Pimpl idiom and hide the private part of your class in an internal struct.
Normally private members don't matter. If I'm looking at a class to determine how to use it somewhere else in code I don't care about it's internals so put private members at the bottom since i don't need to know about them. If I"m modifying a class then I'll take the time to find the private members and know they'll be at the bottom instead of having to skim the entire class.
We are like opposites: My Question
My reasoning is that when you are becoming familiar with a class it is more beneficial to first get introduced to the public interface then go deeper into the private members when you need to. If you start by looking at the private members, you have no context how they are used.
Agreed. Private members should be declared at bottom. The only good reason to declare private members first that i found, is when a function needs to get or return a custom data type, like: vector . The compiler will ask you about what kind of data is that.
But even, in that case i preffer to do:
{
private: /// advicing ofc theres more private below!
earlier declare just of the type
public:
private:
};