- Getters & Setters argument - 18 Updates
- capture member variable in labmda. - 5 Updates
- [ANN] U++ 2015.1 released - 1 Update
- Getters & Setters argument - 1 Update
JiiPee <no@notvalid.com>: Mar 04 01:46PM Two people, Peter and John, arguing. Any comments; which one of them is right and why? Who won the argument?: Peter: I created a nice struct (kind of class) for my coordinates: struct Coordinate { int x; int y; }; John: well, not so nice.... they teach that you should put member variables to private section and create Get and Set functions to get and set their values... Thats what they teach in schools and all the books seems to say that as well. You did not know that?? Peter: Yes most of the time, but lets look at this code: ... int main() { int age = 35; std::cout<<"My age is "<<age; } Should age be inside a class and should we have a Get to get access to it, just like we did with coordinate? Like this: class Integer { public: int Set(int val) { _value = val; } int Get() { return _value; } private: int _value; }; So the code should be (according to you Peter): ... int main() { Integer age; age.Set(35); std::cout<<"My age is "<<age.Get(); } Right??? John: Do you think I am stupid???? or course not!!! Why an earth would you hide integer inside a class like that? Peter: But what fundamental difference is there with age and coordinate variables? Why the other one should be hidden as a private and the other one not?? Tell me.... |
JiiPee <no@notvalid.com>: Mar 04 02:01PM sorry, there is a mistake; should be: struct Point { int x; int y; }; instead of: struct Coordinate { int x; int y; }; like Stefan pointed out. So the original example using Point instead of Coordinate On 04/03/2015 13:46, JiiPee wrote: |
JiiPee <no@notvalid.com>: Mar 04 02:02PM On 04/03/2015 13:54, Stefan Ram wrote: > think about how to implement it. > Sometimes records of public fields are ok, but it depends on the > meaning (contract). Yes, should be "Point" instead. I mention it now there |
jt@toerring.de (Jens Thoms Toerring): Mar 04 03:07PM > }; > like Stefan pointed out. > So the original example using Point instead of Coordinate What about th other question Stefan asked? What's the purpose of this 'Point' structure? If this is to be always used for points with arbitrary coordinates then this will do nicely as it is. On the oter hand, if this is (or might be at some later time) used for points with certain restraints (e.g. only points in a certain quadrant are allowed), or this class is meant as the base class for something like that, then you need a setter in order to be able to control that coordinates only can be modified so that the extra condition is still satisfied. And once you've a setter you also need a getter, of course. And in your 'age' example the same principle applies. To start with it might have been preferable to use an unsigned int instead of an int, since an age normally can't be negative. And if there are extra conditions to that age (e.g. it's the age of a person) a range check might be useful in some circumstannces since there is no known person who's 1000 year old, which then would re- quire to create an 'Age' class with a setter and a getter. Regards, Jens -- \ Jens Thoms Toerring ___ jt@toerring.de \__________________________ http://toerring.de |
JiiPee <no@notvalid.com>: Mar 04 03:30PM On 04/03/2015 15:07, Jens Thoms Toerring wrote: > in order to be able to control that coordinates only can be > modified so that the extra condition is still satisfied. And > once you've a setter you also need a getter, of course. It is meant to be a simple point structure with only few functions, like Move, Increase etc. No plan for use it as a base class. Or possible even so that no functions there, only datamemebers x and y. Used like in most of the graphical systems, like VC++ Point, SFML Point ... a graphical point. The point being that why Point needs to have getters but Integer not (because Integer is type int)? Why dont we also hide ingeters and floats inside a class then? > And in your 'age' example the same principle applies. To start > with it might have been preferable to use an unsigned int instead > of an int, since an age normally can't be negative. No it was not meant to be used only for age but for *all* integers. Like: Integer a, b; a.Set(1); |
JiiPee <no@notvalid.com>: Mar 04 03:40PM On 04/03/2015 15:07, Jens Thoms Toerring wrote: > that, then you need a setter in order to be able to control that > coordinates only can be modified so that the extra condition is still > satisfied. And once you've a setter you also need a getter, of course. The fact that x and y are bundled inside a struct does not mean we must add there functions as well, isnt it? Adding functions are optional/extras. One might say "But if you do not make the getters and setters now, it will be difficult later on to change it". But the other one could argue the same with all int, float, double etc primitive values/types!! For example: int age; Now, one could argue that somewhere in the future we might want an "intelligence age type". So putting age not into a class as a private might cause that later on its very difficult to change it like that if we start now with only: int age; right?? hmmmm.... But if it was like this, then why most of the programmers most of the time do use ints, floats, doubles etc? :) Point being, that if one says that in a struct /class variables must be private, then using the same logic ALL the variable should be embedded inside a class as a private members and then call them from there, like for float: class Float { float get() { return f; } private: float f; }; |
Christopher Pisz <nospam@notanaddress.com>: Mar 04 10:32AM -0600 On 3/4/2015 7:46 AM, JiiPee wrote: > Peter: But what fundamental difference is there with age and coordinate > variables? Why the other one should be hidden > as a private and the other one not?? Tell me.... If I am designing a POD, then I don't mind making member data public. Your Point class as is, is a POD. It doesn't do anything in any method to the data. Once my class has some functionality of some kind in it, then I no longer allow data to be public. -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Christopher Pisz <nospam@notanaddress.com>: Mar 04 10:39AM -0600 On 3/4/2015 9:40 AM, JiiPee wrote: > will be difficult later on to change it". But the other one could argue > the same with all int, float, double etc primitive values/types!! For > example: One needs to look for the find/replace feature in their IDE... > might cause that later on its very difficult to change it like that if > we start now with only: > int age; One needs to work out his design before implementing. There is no might. Either it is or isn't, before coding begins. If one fails to design first, then, again, he can make use of find/replace. But no, if I saw: class { int m_age; public: const int GetAge() const; void SetAge(int age); }; there would be comments in my code review for sure. -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
JiiPee <no@notvalid.com>: Mar 04 04:40PM On 04/03/2015 16:32, Christopher Pisz wrote: > to the data. > Once my class has some functionality of some kind in it, then I no > longer allow data to be public. Almost all Point classes I have studied in graphics they have many functions but they keep x and y public. I made my own Point class as well and desided to put them public Point being that its very simple class and its easier to call obj.x than obj.x(); because x is called so often. Sutter argue though that you do not lose anything by making it a function rather than its a bit longer. He is right, but yes.... its a bit longer call and writing, for me enough to make it public :). Donno about future but with point i still prefer public. |
JiiPee <no@notvalid.com>: Mar 04 04:46PM On 04/03/2015 16:39, Christopher Pisz wrote: >> the same with all int, float, double etc primitive values/types!! For >> example: > One needs to look for the find/replace feature in their IDE... But this argument ("difficult to change it later") I think is used even among top C++ people, I think even Bjarne uses it. >> we start now with only: >> int age; > One needs to work out his design before implementing. Although not everything can be seen on design time. > void SetAge(int age); > }; > there would be comments in my code review for sure. Class name is missing? |
Christopher Pisz <nospam@notanaddress.com>: Mar 04 10:51AM -0600 On 3/4/2015 10:40 AM, JiiPee wrote: > functions but they keep x and y public. I made my own Point class as > well and desided to put them public Point being that its very simple > class and its easier to call True, but are they still PODs? Probably. If you were to make a Get and Set method would anything every go inside them aside from return m_x; or m_x = arg? Probably not. The concept is probably one of those things that come down to opinion and have to be hashed out in coding standards for wherever you work. Mine is if(POD) then public, else protected or private -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Christopher Pisz <nospam@notanaddress.com>: Mar 04 10:56AM -0600 On 3/4/2015 10:46 AM, JiiPee wrote: >>> int age; >> One needs to work out his design before implementing. > Although not everything can be seen on design time. and conversely, we don't need to code for everything that we cannot foresee, otherwise we'd be writing for eons. I've really never had any trouble, even in huge projects doing a find ".m_age" replace ".GetAge()" if needed later. Again, my opinion. >> }; >> there would be comments in my code review for sure. > Class name is missing? It's true, we should put that in the review for that guy too :P -- I have chosen to troll filter/ignore all subthreads containing the words: "Rick C. Hodgins", "Flibble", and "Islam" So, I won't be able to see or respond to any such messages --- |
Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk>: Mar 04 05:39PM On 04/03/2015 13:46, JiiPee wrote: > Peter: But what fundamental difference is there with age and coordinate > variables? Why the other one should be hidden > as a private and the other one not?? Tell me.... I believe I have already told you what the difference is but alas it seems I have to repeat myself. Member variables need only be private if they contribute to the class invariant. The coordinates/point class in the OP as it stands has no class invariant so making the member variables public is perfectly fine. /Flibble |
Geoff <geoff@invalid.invalid>: Mar 04 09:55AM -0800 On Wed, 04 Mar 2015 13:46:38 +0000, JiiPee <no@notvalid.com> wrote: [snip] >Peter: But what fundamental difference is there with age and coordinate >variables? Why the other one should be hidden >as a private and the other one not?? Tell me.... The question you seem to be asking, in a long and tortuous way, is: At what point does an object become complex enough to deserve being a class? Answer: it depends on the abstraction of the object and the design goals for that object. In the case of an integer or any of the intrinsic types, they are probably not complex enough to deserve a class or the usage of the object would be more cumbersome as a class that it is as a naked object. One wouldn't write a class for unsigned int age but one would write a class for object Student, possessing attributes, age, grade, sex, GPA, ID_Number, etc. It's a design choice that is dictated by the scope of the program and it's goals. |
JiiPee <no@notvalid.com>: Mar 04 05:55PM On 04/03/2015 17:39, Mr Flibble wrote: > invariant. The coordinates/point class in the OP as it stands has no > class invariant so making the member variables public is perfectly fine. > /Flibble Yes, I actually agree here. Although for me it also matter what type of structure it is. With point public, but maybe with person details private and getters & setters there. Thats how I might do it. My argument is the one mentioned here, that if: int main() { int a=6; .. } a there is not private class member, then its kind of same as if x and y in Point are not privates. they are kind of similar data types. |
JiiPee <no@notvalid.com>: Mar 04 05:58PM On 04/03/2015 17:55, Geoff wrote: > The question you seem to be asking, in a long and tortuous way, is: > At what point does an object become complex enough to deserve being a > class? oh yes, you got it! :) people seem to be intelligence here...:) > object. One wouldn't write a class for unsigned int age but one would > write a class for object Student, possessing attributes, age, grade, > sex, GPA, ID_Number, etc. yes sure. this makes sense , sure. and thats how I also do these. But am a bit against people who say "ALWAYS" put all data private in classes. |
Bo Persson <bop@gmb.dk>: Mar 04 07:05PM +0100 On 2015-03-04 16:07, Jens Thoms Toerring wrote: > a range check might be useful in some circumstannces since there > is no known person who's 1000 year old, which then would re- > quire to create an 'Age' class with a setter and a getter. The 'Age' is also a bad example, because you don't arbitrarily change the age of a person. There might be a celebrate_birthday() function that adds 1 to the age, but it's hard to see the need to set_age(19) for a guy that is 42. This just shows that there is no general solution, it all depends on what you want to model. Bo Persson |
"Öö Tiib" <ootiib@hot.ee>: Mar 04 12:21PM -0800 On Wednesday, 4 March 2015 15:46:50 UTC+2, JiiPee wrote: > std::cout<<"My age is "<<age.Get(); > } > Right??? No, where is written that human age is integer? Integer without unit, accuracy and limits? To my knowledge it is time interval from moment of birth until now. Birth is such an messy event that it is hardly ever fixed with better accuracy than minute. Also that particular time interval can not be negative, because we can not predict when someone will born in the future. Longest documented human age is 122 years and 164 days. Oldest fictional human Methuselah lived 969 years. So there are clearly limits too. IOW Peter's example clearly shows that numeric data is too complex for him. That does not pass any reviews and so he should go back practicing to output ASCII texts ... "Hello World!" and from there perhaps Visual Basic, PHP ... no way C++. ;) |
Glen Stark <mail@glenstark.net>: Mar 04 01:24PM Hi everyone. Consider the following nonsense example (I'm not trying to do exactly this, but it illustrates my question simply): class Foo { void fun(); int m_val; } Foo::fun() { std::vector foos = get_some_other_foos(); auto non_matching_foo = [m_val](Foo f){return (f.m_val != m_val);}; if(std::find_if(foos.begin(), foos.end(), non_matching_foo)) std::cout << "found a different foo \n"; } } This won't compile because I can't capture m_val. I realize I can capture 'this', but I don't really want to capture 'this', as in reality I just need an int, not everything in the class, and I'd like that to be clear in the lambda declaration. Is there a solution to this? I'd also be interested in hearing why the above isn't allowed. Thanks Glen |
Victor Bazarov <v.bazarov@comcast.invalid>: Mar 04 09:59AM -0500 On 3/4/2015 8:24 AM, Glen Stark wrote: > clear in the lambda declaration. > Is there a solution to this? I'd also be interested in hearing why the > above isn't allowed. I don't have enough time to study the problem in depth it perhaps deserves, but the first thing that came to mind is to provide a functor instead of a lambda to the find_if and construct it with the 'm_val' as its parameter. Something like this (pseudocode): struct check_match { int m_val2check; check_match(int val) : m_val2check(val) {} bool operator()(Foo f1) const { return f1.m_val != m_val2check; } }; find_if(foos.begin(), foos.end(), check_match(this->m_val)); V -- I do not respond to top-posted replies, please don't ask |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Mar 04 04:39PM > capture 'this', but I don't really want to capture 'this', as in reality > I just need an int, not everything in the class, and I'd like that to be > clear in the lambda declaration. You can only capture automatic variables and 'this'. So you could do: void Foo::fun() { std::vector<Foo> foos = get_some_other_foos(); int this_m_val = m_val; auto non_matching_foo = [=](Foo f){ return f.m_val != this_m_val; }; if(std::find_if(foos.begin(), foos.end(), non_matching_foo) != foos.end()) std::cout << "found a different foo \n"; } -- Ben. |
JiiPee <no@notvalid.com>: Mar 04 06:09PM How about using a reference to do it: class Foo { public: void fun(); int m_val; }; void Foo::fun() { int& this_m_val = m_val; auto la = [&](){ this_m_val = 55; }; la(); } This would change m_val to 55; On 04/03/2015 13:24, Glen Stark wrote: |
Chris Vine <chris@cvine--nospam--.freeserve.co.uk>: Mar 04 08:20PM On Wed, 04 Mar 2015 13:24:33 GMT > that to be clear in the lambda declaration. > Is there a solution to this? I'd also be interested in hearing why > the above isn't allowed. Ben Bacarisse has given you the correct answer. Unless you create a local variable, in a member function a lambda expression only captures the 'this' pointer of the object of which the function is a member and not its other members. So with C++11, in the lambda expression you have to address object members through the this pointer if you don't create local copies. C++14 does have a simplification (?) for this, if you happen to use gcc-4.9 or clang-3.4 (-std=c++14 or -std=c++1y), in the sense that it makes it easier to create the local variable. Possibly gcc-4.8 also supports it (I haven't tested). The syntax for this in C++14 is as follows: auto non_matching_foo = [m_val=this->m_val](Foo f){ return (f.m_val != m_val); }; Of course, where the member 'm_val' is expensive to copy, you may be better to stick to the C++11 approach and address the member through the this pointer. Chris |
Mirek Fidler <cxl@ntllib.org>: Mar 04 12:06PM -0800 U++ is BSD licensed C++ cross-platform rapid application development framework focused on programmers productivity without sacrificing runtime performance. http://www.ultimatepp.org New features for release 2015.1 (rev 8227) Core * U++ abandoned "default pick" semantics for containers. The transfer semantics now has to be specified explicitly, either as 'pick' or 'clone' (this is enforced in C++11 mode, in legacy mode old behaviour is maintained and pick/clone are optional). * New containers, based on fast-insertion arrays: InVector, InArray, SortedIndex, SortedVectorMap, SortedArrayMap. SortedIndex, SortedVectorMap and SortedArrayMap provide binary searches, while maintaining random access to the array, with performance comparable to binary tree based containers (e.g. std::map). * New Core/POP3 package - support for retrieving emails form POP3 servers. * Initial round of C++11 support (lambda callbacks, minor fixes) * A set of small utility functions findarg, decode, min/max now supporting more arguments. SplitTo and Merge for splitting/merging strings. * InFilterStream, OutFilterStream classes that can provide some sort of processing (e.g. compression) to regular streams. * New Date related functions EasterDay, GetMonths, GetWeek, GetWeekDate. * MIME / fileextension mapping functions FileExtToMIME and MIMEToFileExt. * Some ARMv7 related optimization (unaligned access, byte swaps). * SpinLock class. * Stream optimization methods GetPtr, PutPtr, GetSzPtr provide safe access to Stream internal buffer. * String/WString::Find(const String&) significantly speed optimized. * Value invalid casts now throwing exception ValueTypeError instead of panic. * WebSocket class. * [Stable]SortBy[Keys|Values] functions for sorting VectorMap, ArrayMap. * plugin/lz4 - adapted fast compression library * plugin/lzma - adapted high ratio compression library * plugin/glew - popular OpenGL utility library adapted as package GUI programming & graphics * Gtk backend (U++ now using Gtk for host platform interface) * EditDateDlg, EditNumber simple function for entering single value (equivalent to EditText) * ArrayCtrl new methods to add/retrieve ValueMaps and ValueArrays, new CreateCtrl<T> method. * LineEdit heavily optimized to be able to work with huge files (>100MB). * LineEdit now able to work with rectangular selections. * DDARasterizer and SDraw are new tools providing Draw with minimal host platform support (only two operations, paint Image and paint colored rectangle, are required). * Image rescaling now supports varios filters, like bicubic or lanczos. * AttrText is now rich comparable Value: it is now possible to sort ArrayCtrl by AttrText filled columns. * GLDraw - Draw implemented using OpenGL (ES). * SVG parser (to Painter interface). * plugin/ppm - trivial image format support. * RichText/RichEdit header/footer support, QTF now using BASE64 for binary data (instead of 7-bit raw encoding). * Turtle is another web framework of U++ - this one allows to convert existing GUI applications into Web application: simple javascript terminal connects to server where GUI application runs. * ScatterDraw/ScatterCtrl - Added legend table, linked scroll controls, units, data fitting to equations, run time user equations, zoom window, improved text drawing, huge datasets handling and key/mouse handling. * X11 backend: Multimonitor support, removed flickering when opening window * Error[OK|Cancel|Yes|No...] - new Prompt variants with Error decorations Sql * New variants of Insert/Select/Update commands (now roughly supporting all important combinations of SQL standard). * SqlSetFrom functions creates a SqlSet from any container. * S_ structures refactored, now provide an improved interface with better introspection. * Sql console redesigned. TheIDE * Win64 PDB (Visual C++ compiled code) debugger. * Find/replace GUI refactored, added support for incremental finds, added regexp support. * Block replace now has grep-like feature to remove matching/non-matching lines (instead of replacing matches) * Case insensitive replace now has "mimic case" option (mimics the case of first and next characters, so replacing "vector" -> "array", when match is "Vector", result is "Array"). * Compilation errors now parsed and displayed nicely. * Assist++ context goto (Alt+J) now supports local variables (goes to declaration). * Layout/Icon designers now have find Search filter. * Ide now displays long numbers with thousands separators. * Syntax highlighting refactored, added highlighting of HTML, XML, .log files, .xsd files, .diff files, C++11 keywords highlighted, TODO and FIXME comments highlighted * Directory comparison tool. * Simple Json/XML viewers. * Comment/Uncomment block. * Selected identifier is now highlighted in all the text. * Current line is highlighted with faint lines. * Precompiled headers support for release mode (BLITZ still far better for debug builds). * Insert file path (plain or as CString) function. * Layout code generator now can place double-quotes around elements, generate handlers for buttons. * Layout designer now can sort widgets based on their graphical position. * Code navigator is completely redesigned. * Icon designer supports various rescaling filters and new icon smoothing function. * Ide now has line duplicate feature (CTRL+D). |
ram@zedat.fu-berlin.de (Stefan Ram): Mar 04 01:54PM >Peter: I created a nice struct (kind of class) for my coordinates: >int x; >int y; It depends on the definition of the meaning of the entity (the contract). »struct Coordinate« is kind of semantic mismatch, because each one (x and y) is called »a coordinate« (singular). So, together, they /are not a coordinate/ (they might be »coordinates« in the plural). This mismatch shows that the author is not clear in what he wants to model. To improve, first write the contract of the struct, then think about how to implement it. Sometimes records of public fields are ok, but it depends on the meaning (contract). |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment