protected

What's the real reason for preventing protected member access through a base/sibling class?

做~自己de王妃 提交于 2019-11-27 14:03:48
I recently discovered that a method in a derived class can only access the base class's protected instance members through an instance of the derived class (or one of its subclasses): class Base { protected virtual void Member() { } } class MyDerived : Base { // error CS1540 void Test(Base b) { b.Member(); } // error CS1540 void Test(YourDerived yd) { yd.Member(); } // OK void Test(MyDerived md) { md.Member(); } // OK void Test(MySuperDerived msd) { msd.Member(); } } class MySuperDerived : MyDerived { } class YourDerived : Base { } I managed to work around this restriction by adding a static

subtle C++ inheritance error with protected fields

眉间皱痕 提交于 2019-11-27 13:51:57
Below is a subtle example of accessing an instance's protected field x. B is a subclass of A so any variable of type B is also of type A. Why can B::foo() access b's x field, but not a's x field? class A { protected: int x; }; class B : public A { protected: A *a; B *b; public: void foo() { int u = x; // OK : accessing inherited protected field x int v = b->x; // OK : accessing b's protected field x int w = a->x; // ERROR : accessing a's protected field x } }; Here is the error I get with g++ $ g++ -c A.cpp A.cpp: In member function ‘void B::foo()’: A.cpp:3: error: ‘int A::x’ is protected A

Why subclass in another package cannot access a protected method?

谁说胖子不能爱 提交于 2019-11-27 13:42:43
I have two classes in two different packages: package package1; public class Class1 { public void tryMePublic() { } protected void tryMeProtected() { } } package package2; import package1.Class1; public class Class2 extends Class1 { doNow() { Class1 c = new Class1(); c.tryMeProtected(); // ERROR: tryMeProtected() has protected access in Class1 tryMeProtected(); // No error } } I can understand why there is no error in calling tryMeProtected() since Class2 sees this method as it inherits from Class1 . But why isn't it possible for an object of Class2 to access this method on an object of Class1

Java: protected access across packages

风流意气都作罢 提交于 2019-11-27 13:05:59
问题 I would like to understand what's happening in the example below (where a protected member is being accessed from outside the package through a subclass). I know for classes outside the package, the subclass can see the protected member only through inheritance. There are two packages: package1 and package2 . package1 : ProtectedClass.java package org.test.package1; public class ProtectedClass { protected void foo () { System.out.println("foo"); } } package2 : ExtendsprotectedClass.java

Should you ever use protected member variables?

可紊 提交于 2019-11-27 10:49:44
Should you ever use protected member variables? What are the the advantages and what issues can this cause? Should you ever use protected member variables? Depends on how picky you are about hiding state. If you don't want any leaking of internal state, then declaring all your member variables private is the way to go. If you don't really care that subclasses can access internal state, then protected is good enough. If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't

Reasons to use private instead of protected for fields and methods

家住魔仙堡 提交于 2019-11-27 10:29:25
问题 This is a rather basic OO question, but one that's been bugging me for some time. I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected . This is because, generally, I don't see any use in hiding the implementation between base class and child class, except when I want to set specific guidelines for the extension of my classes (i.e. in frameworks). For the majority of cases I think trying to limit how my class will be extended either by me or

Why we should not use protected static in java

Deadly 提交于 2019-11-27 10:28:42
I was going through this question Is there a way to override class variables in Java? The first comment with 36 upvotes was: If you ever see a protected static , run. Can anyone explain why is a protected static frowned upon? Tim B It's more a stylistic thing than a direct problem. It suggests that you haven't properly thought through what is going on with the class. Think about what static means: This variable exists at class level, it does not exist separately for each instance and it does not have an independent existence in classes which extend me . Think about what protected means: This

Protected and private methods in Rails

末鹿安然 提交于 2019-11-27 10:10:11
Method visibility in Ruby (public, protected, and private methods) has been well explained in places like this blog post . But in Ruby on Rails it seems slightly different than it would be in a regular Ruby application because of the way the framework is set up. So, in Rails models, controllers, helpers, tests, etc., when is/isn't it appropriate to use protected or private methods? Edit : Thanks for the answers so far. I understand the concept of protected and private in Ruby, but I'm looking more for an explanation of the typical way those types of visibility are used within the context of

What are practical uses of a protected constructor?

百般思念 提交于 2019-11-27 09:18:43
问题 Why would anyone declare a constructor protected? I know that constructors are declared private for the purpose of not allowing their creation on stack. 回答1: When a class is (intended as) an abstract class, a protected constructor is exactly right. In that situation you don't want objects to be instantiated from the class but only use it to inherit from. There are other uses cases, like when a certain set of construction parameters should be limited to derived classes. 回答2: one use could be

Get order item meta data in an unprotected array in Woocommerce 3

落花浮王杯 提交于 2019-11-27 08:12:11
问题 Is there another method to return meta values for custom attributes that doesn't return a protected array foreach ($order->get_items() as $item_key => $item_values) { $item_id = $item_values->get_id(); $item_meta_data = $item_values->get_meta_data(); var_dump($item_meta_data); } It outputs: object(WC_Meta_Data)#3433 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(4690) ["key"]=> string(14) "pa_second-half" ["value"]=> string(11) "nutty-butty" } I've also tried this $item_meta_data