access-specifier

Why can I access private variables in the copy constructor?

放肆的年华 提交于 2019-11-26 10:09:04
问题 I have learned that I can never access a private variable, only with a get-function in the class. But then why can I access it in the copy constructor? Example: Field::Field(const Field& f) { pFirst = new T[f.capacity()]; pLast = pFirst + (f.pLast - f.pFirst); pEnd = pFirst + (f.pEnd - f.pFirst); std::copy(f.pFirst, f.pLast, pFirst); } My declaration: private: T *pFirst,*pLast,*pEnd; 回答1: IMHO, existing answers do a poor job explaining the "Why" of this - focusing too much on reiterating what

Understanding private methods in Ruby

允我心安 提交于 2019-11-26 09:20:54
问题 class Example private def example_test puts \'Hello\' end end e = Example.new e.example_test This of course will not work, because we specified explicit receiver - instance of Example ( e ), and that is against a \"private rule\". But I cannot understand, why one cannot do in Ruby this: class Foo def public_m self.private_m # <= end private def private_m puts \'Hello\' end end Foo.new.public_m The current object inside public_m method definition (i.e. self ) is the instance of Foo. So why it

Why does Ruby have both private and protected methods?

自作多情 提交于 2019-11-26 04:32:52
问题 Before I read this article, I thought access control in Ruby worked like this: public - can be accessed by any object (e.g. Obj.new.public_method ) protected - can only be accessed from within the object itself, as well as any subclasses private - same as protected, but the method doesn\'t exist in subclasses However, it appears that protected and private act the same, except for the fact that you can\'t call private methods with an explicit receiver (i.e. self.protected_method works, but

What is the default access modifier?

随声附和 提交于 2019-11-26 02:08:53
问题 I just started reading a Java book and wondered; which access modifier is the default one, if none is specified? 回答1: The default visibility is known as “private package” (though you can't use this explicitly), which means the field will be accessible from inside the same package to which the class belongs. As mdma pointed out, it isn't true for interface members though, for which the default is "public". See Java's Access Specifiers 回答2: The default specifier depends upon context. For

Difference between private, public, and protected inheritance

谁都会走 提交于 2019-11-25 23:57:12
问题 What is the difference between public , private , and protected inheritance in C++? All of the questions I\'ve found on SO deal with specific cases. 回答1: To answer that question, I'd like to describe member's accessors first in my own words. If you already know this, skip to the heading "next:". There are three accessors that I'm aware of: public , protected and private . Let: class Base { public: int publicMember; protected: int protectedMember; private: int privateMember; }; Everything that