member-variables

Is returning references of member variables bad practice?

此生再无相见时 提交于 2019-11-26 18:01:52
问题 The below is said to be better then having first/second as public members. I believe this is nearly as bad. If you're giving a way to access a private variable outside of the class then whats the point? Shouldn't the functions be T First(); void(or T) First(const T&) Sample: // Example 17-3(b): Proper encapsulation, initially with inline accessors. Later // in life, these might grow into nontrivial functions if needed; if not, then not. // template<class T, class U> class Couple { Couple() :

Can member variables be used to initialize other members in an initialization list?

◇◆丶佛笑我妖孽 提交于 2019-11-26 13:07:20
问题 Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int evenSum; public: Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB) { } }; When i instanciate Foo like this: Foo foo(1,3); then evenA is 0, evenB is 2, but will evenSum be initialized to 2? I tried this on my current platform (iOS) and it seems to work, but I\'m not sure whether this code is portable. Thanks for your help! 回答1: This is well-defined and portable, 1 but it's

Python Class Members

有些话、适合烂在心里 提交于 2019-11-26 10:28:52
问题 I am just learning Python and I come from a C background so please let me know if I have any confusion / mix up between both. Assume I have the following class: class Node(object): def __init__(self, element): self.element = element self.left = self.right = None @classmethod def tree(cls, element, left, right): node = cls(element) node.left = left node.right = right return node This is a class named Node , that overloads the constructor, to be able to handle different arguments if needed.

Private members in Python

邮差的信 提交于 2019-11-26 06:34:49
问题 How can I make methods and data members private in Python? Or doesn\'t Python support private members? 回答1: 9.6. Private Variables “Private” instance variables that cannot be accessed except from inside an object, don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). It should be considered an

What is the difference between class and instance attributes?

那年仲夏 提交于 2019-11-25 21:43:01
问题 Is there any meaningful distinction between: class A(object): foo = 5 # some default value vs. class B(object): def __init__(self, foo=5): self.foo = foo If you\'re creating a lot of instances, is there any difference in performance or space requirements for the two styles? When you read the code, do you consider the meaning of the two styles to be significantly different? 回答1: Beyond performance considerations, there is a significant semantic difference. In the class attribute case, there is