protected

Why do we actually need Private or Protected inheritance in C++?

為{幸葍}努か 提交于 2019-11-26 21:19:47
In C++, I can't think of a case in which I would like to inherit private/protected from a base class: class Base; class Derived1 : private Base; class Derived2 : protected Base; Is it really useful? It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement class Engine { public: Engine(int numCylinders); void start(); // Starts this Engine }; class Car { public: Car() : e_(8) { } //

Why can a class not be defined as protected?

孤街浪徒 提交于 2019-11-26 18:57:57
问题 I know this is a stupid question, but I still have a doubt which needs to be cleared. My question is, why can we not define a class as protected ? I know that we can't, but why? There should be some specific reason. 回答1: Because it makes no sense. Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses. Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class

Why can't I access C# protected members except like this?

倖福魔咒の 提交于 2019-11-26 18:52:49
This code: abstract class C { protected abstract void F(D d); } class D : C { protected override void F(D d) { } void G(C c) { c.F(this); } } Generates this error: Cannot access protected member 'C.F(D)' via a qualifier of type 'C'; the qualifier must be of type 'D' (or derived from it) What in the world were they thinking? (Would altering that rule break something?) And is there a way around that aside from making F public? Edit: I now get the reason for why this is (Thanks Greg ) but I'm still a bit perplexed as to the rational; given: class E : C { protected override void F(D d) { } } Why

How to lock Excel cells in VBA?

旧城冷巷雨未停 提交于 2019-11-26 18:42:19
问题 I have an Excel worksheet that acts like an application, with form control buttons allowing users to 'navigate' through records. First, Previous, Next & Last cycle appropriately through one of the worksheets records, displaying the values in my 'form' worksheet. When users are not in Edit or Add Mode, I would like to lock the cells to prevent users from modifying contents. I tried Range("A1:O24").Locked = True, but I am still able to type new values into the cells. Anyone know how to

How to open a password protected excel file using python?

痴心易碎 提交于 2019-11-26 17:20:50
问题 I looked at the previous threads regarding this topic, but they have not helped solve the problem. how to read password protected excel in python How to open write reserved excel file in python with win32com? I'm trying to open a password protected file in excel without any user interaction. I searched online, and found this code which uses win32com.client When I run this, I still get the prompt to enter the password... from xlrd import * import win32com.client import csv import sys xlApp =

Protected data in parent class not available in child class?

爷,独闯天下 提交于 2019-11-26 17:01:22
问题 I am confused: I thought protected data was read/writable by the children of a given class in C++. The below snippet fails to compile in MS Compiler class A { protected: int data; }; class B : public A { public: B(A &a) { data = a.data; } }; int main() { A a; B b = a; return 0; } Error Message: Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. demoFail.cpp demoFail.cpp(12) : error C2248: 'A::data' : cannot

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

╄→尐↘猪︶ㄣ 提交于 2019-11-26 16:35:30
问题 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 :

subtle C++ inheritance error with protected fields

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 16:31:18
问题 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++

Should you ever use protected member variables?

本小妞迷上赌 提交于 2019-11-26 15:20:06
问题 Should you ever use protected member variables? What are the the advantages and what issues can this cause? 回答1: 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

Protected and private methods in Rails

醉酒当歌 提交于 2019-11-26 15:02:51
问题 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