member-variables

Suppress unused variable warning in C++ => Compiler bug or code bug?

情到浓时终转凉″ 提交于 2019-11-30 02:30:09
问题 Presently, I am using the following function template to suppress unused variable warnings: template<typename T> void unused(T const &) { /* Do nothing. */ } However, when porting to cygwin from Linux, I am now getting compiler errors on g++ 3.4.4 (On linux I am 3.4.6, so maybe this is a bug fix?): Write.cpp: In member function `void* Write::initReadWrite()': Write.cpp:516: error: invalid initialization of reference of type 'const volatile bool&' from expression of type 'volatile bool' ../..

How to implement a read-only member variable in PHP?

自古美人都是妖i 提交于 2019-11-28 09:19:29
When trying to change it,throw an exception. I suppose a solution, for class properties, would be to : not define a property with the name that interests you use the magic __get method to access that property, using the "fake" name define the __set method so it throws an exception when trying to set that property. See Overloading , for more informations on magic methods. For variables, I don't think it's possible to have a read-only variable for which PHP will throw an exception when you're trying to write to it. For instance, consider this little class : class MyClass { protected $_data =

Property and Encapsulation

半世苍凉 提交于 2019-11-28 00:16:44
Following is a question regarding using properties in class. I have been using public properties instead of exposing member variables publically. Majority advise that this approach helps encapsulation. However, I don’t understand the encapsulation advantage by making it a property. many people donot know the real reason for using properties. They just do it as part of coding standard. Can someone clearly explain how a property is better than public member variable and how it improves encapsulation? Encapsulation helps by insulating calling classes from changes. Let's imagine you have a simple

Is returning references of member variables bad practice?

半腔热情 提交于 2019-11-27 18:28:36
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() : deleted_(false) { } T& First() { return first_; } U& Second() { return second_; } void MarkDeleted() {

Is C++ static member variable initialization thread-safe?

爱⌒轻易说出口 提交于 2019-11-27 12:06:53
According to following resources, in C++(Specially Visual C++) scoped static variable initialization isn't thread safe. But, global static variables are safe. Thread-safe static variables without mutexing? http://blogs.msdn.com/oldnewthing/archive/2004/03/08/85901.aspx So, is following code with static member variable thread-safe? class TestClass { public: static MyClass m_instance; } Myclass TestClass::m_instance; Thanks in advance! It's more a question of function-scoped static variables vs. every other kind of static variable, rather than scoped vs. globals. All non-function-scope static

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

半世苍凉 提交于 2019-11-27 12:05:06
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! This is well-defined and portable, 1 but it's potentially error-prone. Members are initialized in the order they're declared in the class body, not the order

Python Class Members

人盡茶涼 提交于 2019-11-27 06:48:19
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. What is the difference between defining self.element in __init__ only (as shown above) as opposed to doing

How to implement a read-only member variable in PHP?

橙三吉。 提交于 2019-11-27 02:49:50
问题 When trying to change it,throw an exception. 回答1: I suppose a solution, for class properties, would be to : not define a property with the name that interests you use the magic __get method to access that property, using the "fake" name define the __set method so it throws an exception when trying to set that property. See Overloading, for more informations on magic methods. For variables, I don't think it's possible to have a read-only variable for which PHP will throw an exception when you

Property and Encapsulation

邮差的信 提交于 2019-11-26 23:24:20
问题 Following is a question regarding using properties in class. I have been using public properties instead of exposing member variables publically. Majority advise that this approach helps encapsulation. However, I don’t understand the encapsulation advantage by making it a property. many people donot know the real reason for using properties. They just do it as part of coding standard. Can someone clearly explain how a property is better than public member variable and how it improves

Private members in Python

折月煮酒 提交于 2019-11-26 22:07:32
How can I make methods and data members private in Python? Or doesn't Python support private members? jbochi 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 implementation detail and subject to change without notice. Since there is a valid use-case for class-private