getter

Why having const and non-const accessors?

时光怂恿深爱的人放手 提交于 2019-11-29 14:47:14
Why do the STL containers define const and non-const versions of accessors ? What is the advantage of defining const T& at(unsigned int i) const and T& at(unsigned int) and not only the non-const version ? Because you wouldn't be able to call at on a const vector object. If you only had the non- const version, the following: const std::vector<int> x(10); x.at(0); would not compile. Having the const version makes this possible, and at the same time prevents you from actually changing what at returns - which is by contract, since the vector is const . The non- const version can be called on a

In Java is there a performance difference between referencing a field through getter versus through a variable?

纵饮孤独 提交于 2019-11-29 14:40:00
Is there any differences between doing Field field = something.getSomethingElse().getField(); if (field == 0) { //do something } somelist.add(field); versus if (something.getSomethingElse().getField() == 0) { //do something } somelist.add(something.getSomethingElse().getField()); Do references to the field through getters incur a performance penalty or is it the same as referencing an assigned variable? I understand that the variable is just a reference to the memory space, so the getter should just be another way to get at that memory space. Note that this is an academic question (school of

speed of getter function vs direct access

元气小坏坏 提交于 2019-11-29 12:05:08
问题 I have recently begun using more getter functions as opposed to direct access to make my code more flexible. I am curious what the cost of this is in terms of speed. Suppose that earth is an object and we have the following parent object: var star={} star.planet=earth star.getPlanet=function(){ return this.planet } Is there a non-negligible difference in speed between the following two statements? print(star.planet) print(star.getPlanet()) 回答1: In V8: A function that is so short and doesn't

JSR 303 Bean Validation - Why on getter and not setter?

三世轮回 提交于 2019-11-29 10:46:29
问题 I don't understand why JSR 303 (bean validation) is for the getter methods and not setter? Isn't it more logical to put it under setter method since that is the entry point into a field and validation should be checked prior to that? 回答1: Annotating getters doesn't mean that validation is performed when a getter is invoked. It is just used to identify the property to which a constraint shall apply. The big advantage of putting constraints on (usually public) getters instead on (typically

The use of getters and setters for different programming languages [closed]

╄→尐↘猪︶ㄣ 提交于 2019-11-29 07:42:53
So I know there are a lot of questions on getters and setters in general, but I couldn't find something exactly like my question. I was wondering if people change the use of get/set depending on different languages. I started learning with C++ and was taught to use getters and setters. This is what I understand: In C++ (and Java?), a variable can either be public or private, but we cannot have a mix. For example, I can't have a read-only variable that can still be changed inside the class. It's either all public (can read and change it), or all private (can't read and can only change inside

Why do people write private-field getters returning a non-const reference?

坚强是说给别人听的谎言 提交于 2019-11-29 06:00:55
We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int& integer() { return integer_; } string& someString() { return someString_; } // other "functions" } int main() { foo f; f.integer() = 10; f.someString() = "something"; return 0; } I have seen this being used in many places and I don't get why. Basically it returns a reference to the data and thus exposes it directly to the outside. So encapsulation is not really

Objective-C getter/ setter

本秂侑毒 提交于 2019-11-29 04:01:26
I'm trying to work my way through an Objective-C tutorial. In the book there is this example: @interface { int width; int height; XYPoint *origin; } @property int width, height; I thought, "hey there's no getter/setter for the XYPoint object. The code does work though." Now i'm going maybe to answer my own question :). I thinks its because "origin" is a pointer already, and whats happening under the hood with "width" and "height", is that there is going te be created a pointer to them.. Am i right, or am i talking BS :) ?? I just dont get it. here's main: #import "Rectangle.h" #import "XYPoint

How to define dynamic setter and getter using reflection?

梦想与她 提交于 2019-11-29 01:36:06
I've a list of strings, field names, of a class in a loop from resource bundle. I create an object and then using loop i want to set values for that object. For example, for object Foo f = new Foo(); with parameter param1, I have string "param1" and I somehow want to concate "set" with it like "set"+"param1" and then apply it on f instance as: f.setparam1("value"); and same for getter. I know reflection will help but I couldn't manage to do it. Please help. Thanks! You can do something like this. You can make this code more generic so that you can use it for looping on fields: Class aClass = f

boolean properties starting with “is” does not work

笑着哭i 提交于 2019-11-28 23:53:15
I have a project that use JSF 2.1 and PrimeFaces. I tried to use a simple <h:outputText> referencing #{myBean.matriz} and I got this error: SEVERE: javax.el.PropertyNotFoundException: ... value="#{myBean.matriz}": Missing Resource in EL implementation: ???propertyNotReadable??? The getter is: isMatriz() . Should it be getMatriz() ? BalusC The is prefix works only for boolean , not Boolean . You've there apparently actually a Boolean property. You've 2 options to fix it: Rename the getter with get prefix. Replace Boolean by boolean . Note that it will default to false instead of null . See also

Make a property that is read-only to the outside world, but my methods can still set

这一生的挚爱 提交于 2019-11-28 23:42:39
问题 In JavaScript (ES5+), I'm trying to achieve the following scenario: An object (of which there will be many separate instances) each with a read-only property .size that can be read from the outside via direct property read, but cannot be set from the outside. The .size property must be maintained/updated from some methods which are on the prototype (and should stay on the prototype). My API is already defined by a specification so I can't modify that (I'm working on a polyfill for an already