Is defining get/set methods for every field overkill?

Siarhei Siamashka siarhei.siamashka at gmail.com
Tue Nov 22 00:46:34 UTC 2022


On Monday, 21 November 2022 at 23:41:22 UTC, thebluepandabear 
wrote:
> But why give a C++ code example? 🤨

It's a D code example and it even can't be compiled by a C++ 
compiler. Just add the missing main function:

```D
void main()
{
   shared c = new Counter;
   c.incrementCounter;

   c.count = 12345; // haha, this isn't really private in D 
(unless the class
                    // is moved to a different module in its own 
personal file)

   writeln(c.displayCounter);
}
```

The `counter` member variable isn't really protected from rogue 
accesses (if these accesses are done by the code in the same 
source file) and this behavior differs from C++.


More information about the Digitalmars-d-learn mailing list