encapsulation

Cannot access protected member of another instance from derived type's scope

北城以北 提交于 2019-12-01 21:12:35
In this answer to the question " Why can't my object access protected members of another object defined in common base class? ", one can read: You can only access protected members from your own base class instance. Either I don't get it correctly or the following MCVE (live on coliru) proves it wrong: struct Base { void f(); protected: int prot; }; struct Derived : Base { void g(); private: int priv; }; void Base::f() { Base b; b.prot = prot; (void) b; } void Derived::g() { { Derived d; (void) d.priv; } { Derived& d = *this; (void) d.priv; } { Derived d; (void) d.prot; // <-- access to other

Why not use 'protected' or 'private' in PHP?

心已入冬 提交于 2019-12-01 16:46:47
I've been working with the Joomla framework and I have noticed that they use a convention to designate private or protected methods (they put an underscore " _ " in front of the method name), but they do not explicitly declare any methods public , private , or protected . Why is this? Does it have to do with portability? Are the public , private , or protected keywords not available in older versions of PHP? public, private and protected are PHP5 keywords. unfortunately, PHP4 still has a very high install base (especially amongst shared hosting services). here's a pretty pic showing july usage

Can I use private instance methods as callbacks?

痞子三分冷 提交于 2019-12-01 13:50:58
问题 My particular scenario involves doing some text transformation using regular expressions within a private method. The private method calls preg_replace_callback, but is seems that callbacks need to be public on objects, so I'm stuck breaking out of the private world and exposing implementation details when I'd rather not. So, in a nutshell: Can I use an instance method as a callback without losing encapsulation? Thanks. 回答1: Yes, it seems you can: <?php //this works class a { private function

Modifying a class to encapsulate instead of inherit

陌路散爱 提交于 2019-12-01 13:30:43
问题 The codebase I've been handed to work with features a databse class that inherits from MDB2. This forms the basis for the MVC framework in use (a custom built affair) and the models in turn inherit from db. As I'm sure some of you have noticed, this leads to a rather big problem. Every time you instantiate a model, the result is a new database connection being created. This is obviously quite wasteful. It also means that I'm unable to use transactions as intended, because if a transaction

Separation of function declaration and definition in Swift

断了今生、忘了曾经 提交于 2019-12-01 04:38:36
问题 I'm having a look at the new Swift. I come from C, C++, Objective-C... I notice that in swift is not possible (?) to separate the declaration and the definition of functions. The result of this is that structure and class declarations are very long and bloated, and it is therefore difficult to have a "quick picture" of the class by looking at the code. Am I doing something wrong? Is there any approach to overcome this problem, besides trying to make functions small, etc.? Thanks in advance

Can There Be Private Extension Methods?

不羁岁月 提交于 2019-12-01 02:03:08
Let's say I have a need for a simple private helper method, and intuitively in the code it would make sense as an extension method. Is there any way to encapsulate that helper to the only class that actually needs to use it? For example, I try this: class Program { static void Main(string[] args) { var value = 0; value = value.GetNext(); // Compiler error } static int GetNext(this int i) { return i + 1; } } The compiler doesn't "see" the GetNext() extension method. The error is: Extension method must be defined in a non-generic static class Fair enough, so I wrap it in its own class, but still

Encapsulating in JavaScript, does it exist?

泄露秘密 提交于 2019-12-01 01:53:22
I have an experience with the C# programming language, but I also have to work with the JS now and it's rather new for me. I have tried to develop a simple class emulation in JS, like this: http://jsfiddle.net/T74Zm/ function A( inputValue ) { this.Init( inputValue ); this.Print(); } A.prototype = { value: null, Init: function( inputValue ) { this.value = inputValue; }, Print: function () { console.log( this.value ); } } var obj = new A(40); I've tried to encapsulate the variable value in A.prototype , but it seems to be by JavaScript specification that all objects are available. So my

Encapsulation C# newbie

混江龙づ霸主 提交于 2019-11-30 23:39:05
New to C#, and I understand that encapsulation is just a way of "protecting data". But I am still unclear. I thought that the point of get and set accessors were to add tests within those methods to check to see if parameters meet certain criteria, before allowing an external function to get and set anything, like this: private string myName; public string MyName;// this is a property, speical to c#, which sets the backing field. private string myName = "mary";// the backing field. public string MyName // this is a property, which sets/gets the backing field. { get { return myName; } set { if

Design pattern for multiple output formats

☆樱花仙子☆ 提交于 2019-11-30 20:27:34
I have a class structure which represents (internally) the data I wish to output to a file. Some of the member variables are private to the data class so that it can manage itself and stop things going awry. I then want this data to be output into a number of file formats. I could do something like savefile_formatA(DataClass* pDataClass, ofstream& fout); savefile_formatB(DataClass* pDataClass, ofstream& fout); except that the functions need to then see the private member variables of DataClass . I could of course just make savefile_formatXYZ() friend functions but then I would need to add a

Encapsulating in JavaScript, does it exist?

孤街浪徒 提交于 2019-11-30 20:10:05
问题 I have an experience with the C# programming language, but I also have to work with the JS now and it's rather new for me. I have tried to develop a simple class emulation in JS, like this: http://jsfiddle.net/T74Zm/ function A( inputValue ) { this.Init( inputValue ); this.Print(); } A.prototype = { value: null, Init: function( inputValue ) { this.value = inputValue; }, Print: function () { console.log( this.value ); } } var obj = new A(40); I've tried to encapsulate the variable value in A