encapsulation

R6Class - Encapsulation issue: Bad design?

自作多情 提交于 2021-02-07 10:49:17
问题 Minimal example Attaching R6 package require(R6) Element class definition element_factory <- R6Class( "Element", private = list( ..value = 0), active = list( value = function(new) { if(missing(new)) private$..value else private$..value <- new})) Container class definition container_factory <- R6Class( "Container", private = list( ..element = element_factory$new() ), active = list( element = function() private$..element)) Creating container istance co <- container_factory$new() Accessing

Non-member vs member functions in Python

浪子不回头ぞ 提交于 2021-02-07 04:52:28
问题 I'm relatively new to Python and struggling to reconcile features of the language with habits I've picked up from my background in C++ and Java. The latest issue I'm having has to do with encapsulation, specifically an idea best summed up by Item 23 of Meyer's " Effective C++ ": Prefer non-member non-friend functions to member functions. Ignoring the lack of a friend mechanism for a moment, are non-member functions considered preferable to member functions in Python , too? An obligatory,

What is the difference between protected and public variable in python

荒凉一梦 提交于 2021-01-29 15:23:58
问题 In python, what is the difference between protected and public variable in a class class A: def __init__(self): self._protected="protected" self.__private="private" self.public="public" >>> a = A() >>> a.public 'public' >>> a._protected 'protected' >>> Can someone please explain me the difference, and guide me on how to use protected variable in python [In case my method is usage is false] Thanks in Advance. 回答1: None of those terms except "public" really apply in Python. The "private"

Access private member vector of multiple objects in other class

﹥>﹥吖頭↗ 提交于 2021-01-28 13:34:07
问题 I have two classes (A & B) with a similar structure, which both contain a vector of structs. class A/B{ private: std::vector<DataStruct> vec_A/vec_B; ... public: ... } To create/update an object of B, I have to combine the data from the vectors of multiple objects of class A (combining similar entries in the vector into one entry in the vector of object B and do data transformations in B). How do I do this? My thoughts were: Making class B a friend of A. But I've heard, that making classes

Access private member vector of multiple objects in other class

谁说我不能喝 提交于 2021-01-28 13:33:12
问题 I have two classes (A & B) with a similar structure, which both contain a vector of structs. class A/B{ private: std::vector<DataStruct> vec_A/vec_B; ... public: ... } To create/update an object of B, I have to combine the data from the vectors of multiple objects of class A (combining similar entries in the vector into one entry in the vector of object B and do data transformations in B). How do I do this? My thoughts were: Making class B a friend of A. But I've heard, that making classes

Ada encapsulation and private types

蹲街弑〆低调 提交于 2021-01-28 12:12:11
问题 From the compiler's point of view, what's the difference between declaring an Ada type in a package spec or doing it inside the body? 回答1: Generally it is a good practice to make declarations (of types, but also other items like constants or subprograms) the most local possible. In your case if the type is used only in the body and not for users of your package specification (even as private type), put it in the body. Furthermore, if it is used only in a subprogram of the body, put it in that

What are the differences between the multiple ways to create zero-sized structs?

寵の児 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

杀马特。学长 韩版系。学妹 提交于 2020-12-02 05:54:27
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

What are the differences between the multiple ways to create zero-sized structs?

自作多情 提交于 2020-12-02 05:54:04
问题 I found four different ways to create a struct with no data: struct A{} // empty struct / empty braced struct struct B(); // empty tuple struct struct C(()); // unit-valued tuple struct struct D; // unit struct (I'm leaving arbitrarily nested tuples that contain only () s and single-variant enum declarations out of the question, as I understand why those shouldn't be used). What are the differences between these four declarations? Would I use them for specific purposes, or are they

Why protected method is not accessible from subclass?

浪尽此生 提交于 2020-12-01 06:54:10
问题 Consider the following code snippets: package vehicle; public abstract class AbstractVehicle { protected int speedFactor() { return 5; } } package car; import vehicle.AbstractVehicle; public class SedanCar extends AbstractVehicle { public static void main(String[] args) { SedanCar sedan = new SedanCar(); sedan .speedFactor(); AbstractVehicle vehicle = new SedanCar(); // vehicle //WON'T compile // .speedFactor(); } } SedanCar is a subclass of AbstractVehicle which contains a protected method