protected

Unit testing C# protected methods

爷,独闯天下 提交于 2019-12-18 10:31:56
问题 I come from the Java EE world but now I'm working on a .Net project. In Java when I wanted to test a protected method it was quite easy, just having the test class with the same package name was enough. Is there anything similar for C#? Is there any good practice for unit testing the protected methods? I only found frameworks and people saying that I should test only public methods. It should be possible to do it without any framework… 回答1: You can inherit the class you are testing on your

Why can a derived class not access a protected member of its base class through a pointer to base?

若如初见. 提交于 2019-12-18 08:33:34
问题 This is the code: class TestA { protected: int test=12; public: TestA() { cout << "test a: " << test << endl; } ~TestA() { } }; class TestB : public TestA { public: TestB(TestA *testA) { cout << "test b: " << testA->test; } ~TestB() { } }; int main () { TestA *pTestA=new TestA(); TestB *pTestB=new TestB(pTestA); } I'm trying to access of a protected member using a pointer pointing to a TestA type object (thus, an instance of TestA ). TestB is also derived from TestA Why I can't access to it?

Why does protected inheritance cause dynamic_cast to fail?

守給你的承諾、 提交于 2019-12-18 03:46:08
问题 I changed my C++ base class to be protected inheritance and my dynamic_cast (s) stopped working. Why should changing the inheritance to protected change the behavior of dynamic_cast ? struct Base { static Base *lookupDerived(); // Actually returns a Derived * object. }; struct Derived : protected /* Switch this to public to get it working */ Base { static void test() { Base *base = lookupDerived(); if (dynamic_cast<Derived *>(base)) { std::cout << "It worked (we must be using public

Best practices to test protected methods with PHPUnit (on abstract classes)

折月煮酒 提交于 2019-12-17 22:16:30
问题 With PHPUnit and PHP >= 5.3 it is possible to test protected methods. The following page at stackoverflow outlined the best practice on it: "Best practices to test protected methods with PHPUnit" protected static function callProtectedMethod($name, $classname, $params) { $class = new ReflectionClass($classname); $method = $class->getMethod($name); $method->setAccessible(true); $obj = new $classname($params); return $method->invokeArgs($obj, $params); } To test public methods on abstract

Why is Java's AbstractList's removeRange() method protected?

六月ゝ 毕业季﹏ 提交于 2019-12-17 21:27:16
问题 Does anyone have any idea, why removeRange method in AbstractList (and also in ArrayList) is protected ? It looks like a quite well-defined and useful operation, but still, to use it, we're forced to subclass the List implementation. Is there some hidden rationale? Seems quite inexplicable to me. 回答1: Yes, because that's not how you remove a range from outside code. Instead, do this: list.subList(start, end).clear(); This actually calls removeRange behind the scenes. † The OP asks why

Is protected method in super class visible in sub class in a different package? [duplicate]

送分小仙女□ 提交于 2019-12-17 21:12:31
问题 This question already has answers here : What is the difference between public, protected, package-private and private in Java? (28 answers) Closed 2 years ago . It seems very silly, but I am really confused. Please see below code: package com.one; public class SuperClass { protected void fun() { System.out.println("base fun"); } } ---- package com.two; import com.one.SuperClass; public class SubClass extends SuperClass{ public void foo() { SuperClass s = new SuperClass(); s.fun(); // Error

Protected fields not visible to subclasses

喜欢而已 提交于 2019-12-17 16:06:43
问题 I'm writing a custom view that directly extends android.view.View . If I try to access fields mScrollX or mScrollY , I see an error that the field "cannot be resolved or is not a field." The source code for android.view.View has mScrollX, mScrollY, and similar variables declared protected . How is it that my direct subclass cannot access protected fields of its parent class? (Classes like ScrollView apparently can.) P.S. I realize that I can call getScrollX() , but I want to update these

Access to method pointer to protected method?

淺唱寂寞╮ 提交于 2019-12-17 12:47:54
问题 This code: class B { protected: void Foo(){} } class D : public B { public: void Baz() { Foo(); } void Bar() { printf("%x\n", &B::Foo); } } gives this error: t.cpp: In member function 'void D::Bar()': Line 3: error: 'void B::Foo()' is protected Why can I call a protected method but not take its address? Is there a way to mark something fully accessible from derived classes rather than only accessible from derived classes and in relation to said derived class? BTW: This looks related but what

Why subclass in another package cannot access a protected method?

房东的猫 提交于 2019-12-17 10:51:33
问题 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

accessing a protected member of a base class in another subclass

蓝咒 提交于 2019-12-17 06:38:05
问题 Why does this compile: class FooBase { protected: void fooBase(void); }; class Foo : public FooBase { public: void foo(Foo& fooBar) { fooBar.fooBase(); } }; but this does not? class FooBase { protected: void fooBase(void); }; class Foo : public FooBase { public: void foo(FooBase& fooBar) { fooBar.fooBase(); } }; On the one hand C++ grants access to private/protected members for all instances of that class, but on the other hand it does not grant access to protected members of a base class for