ram@zedat.fu-berlin.de (Stefan Ram): Sep 30 10:47PM >One presumes class data members would be private, else what's >the point of having a class? Usually data members are public, as - for example - in, 20.3.2 Class template pair namespace std { template< class T1, class T2 > struct pair { T1 first; T2 second; ... }} But, I agree that it's better to make data members private /if/ the class should happen to have invariants. >Given that, putting the data members first would be contrary >to the generally accepted public, protected, private ordering >with a class. One needs to know the data members first to understand the constructors and member functions. How else can one judge whether /all/ data members are properly initialized in the constructor when one has not yet read their declarations, for example? The data members are the key to understanding the implementation. Therefore, they must be declared first. Then come the lower-level functions and finally the higher- level functions (bottom-up). The lower-level functions are more close to the data member. But very-high level functions of the class, who do not need direct access to the data members should be non-members. |
woodbrian77@gmail.com: Sep 30 01:21PM -0700 On Tuesday, September 22, 2015 at 3:42:24 PM UTC-5, Lynn McGuire wrote: > contributions from experts at CERN, Microsoft, Morgan Stanley, and several other organizations. The guidelines are currently in a > "0.6" state, and contributions are welcome. As Stroustrup said: "We need help!"" > Lynn I disagree with this one NL.16: Use a conventional class member declaration order Reason: A conventional order of members improves readability. When declaring a class use the following order types: classes, enums, and aliases (using) constructors, assignments, destructor functions data Used the public before protected before private order. Private types and functions can be placed with private data. --------------------------------------------- I suggest to make the data members first. Brian Ebenezer Enterprises - 'Is it not lawful for me to do what I wish with what is my own? Or is your eye envious because I am generous?' "So the last shall be first, and the first last." Matthew 20:15,16 http://webEbenezer.net |
maddoxr@acm.org: Sep 30 01:55PM -0700 > envious because I am generous?' "So the last shall > be first, and the first last." Matthew 20:15,16 > http://webEbenezer.net One presumes class data members would be private, else what's the point of having a class? Given that, putting the data members first would be contrary to the generally accepted public, protected, private ordering with a class. You are not suggesting public data members I am certain. Randy. |
Paul <pepstein5@gmail.com>: Sep 30 01:37AM -0700 In the code below, why isn't {3,4} recognized immediately as a vector<int>? Why does it fail to compile if I replace if(vec == std::vector<int>({3,4})) by if(vec == {3,4}) ? Many thanks for your help. Paul void printVectorIfSpecial(const std::vector<int>& vec) { if(vec == std::vector<int>({3,4})) { // do something } } |
bartekltg <bartekltg@gmail.com>: Sep 30 02:22PM +0200 On 30.09.2015 10:37, Paul wrote: > // do something > } > } There is no type (of the class) deduction based on a constructor. And this is the main reason, why we have a _function_ make_pair. pair {1,2} do not work. Either pair<int> {1,2} or make_pair(1,2) bartekltg |
bartekltg <bartekltg@gmail.com>: Sep 30 02:26PM +0200 On 30.09.2015 14:22, bartekltg wrote: > And this is the main reason, why we have a _function_ make_pair. > pair {1,2} do not work. > Either pair<int> {1,2} or make_pair(1,2) OK, this is wrong answer. A correct one, but for different question ;-) {1,2,3} is an iniclializer list, not a vector. vector i c++ is just z class, not a spacjal type in language. You can not use vector in you program (but why you would;)) and {1,2} still is the same. bartekltg |
Martin Shobe <martin.shobe@yahoo.com>: Sep 30 07:57AM -0500 On 9/30/2015 3:37 AM, Paul wrote: > In the code below, why isn't {3,4} recognized immediately as a vector<int>? Because it's not a vector, it's an initializer list. > Why does it fail to compile if I replace if(vec == std::vector<int>({3,4})) by if(vec == {3,4}) ? Because there's no equality operator defined for vectors and initializer lists. > // do something > } > } Martin Shobe |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Sep 30 02:18PM +0100 > In the code below, why isn't {3,4} recognized immediately as a > vector<int>? The main problem is this one: > Why does it fail to compile if I replace if(vec == > std::vector<int>({3,4})) by if(vec == {3,4}) ? It fails to compiler because it's not syntactically valid -- the {...} form is an initialiser list and that just can't occur in that position. An IL is not a valid primary expression. So your questions are really why does C++ not permit an IL to be a primary expression, and, if C++ did permit it, would it be able to find the correct operator== function to call? I can't give you a good answer to either question. I suspect that making ILs into primary expressions would introduce a vast rats nest of complications, but that's not much more than a guess. <snip> -- Ben. |
Paul <pepstein5@gmail.com>: Sep 30 06:48AM -0700 On Wednesday, September 30, 2015 at 2:19:02 PM UTC+1, Ben Bacarisse wrote: > I can't give you a good answer to either question. I suspect that > making ILs into primary expressions would introduce a vast rats nest of > complications, but that's not much more than a guess. Thanks, Ben and everyone else. My posting was in no sense intended as a "Why doesn't C++ allow...?" type of question. I didn't know why I got the error and was trying to avoid similar problems in future. So, suppose that I want code which says "do something if the vec is {3, 4}" Is my code (which works) best practice, or should I avoid casting? If so, how? Many thanks for your help, Paul |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 30 06:44PM +0200 On 9/30/2015 10:37 AM, Paul wrote: > // do something > } > } I'm not sure. Possibly due to pure grammar issues. Consider that the following works as intended: <code> #include <assert.h> #include <vector> #include <stdio.h> void printVectorIfSpecial(const std::vector<int>& vec) { if( operator==( vec, {3,4} ) ) { assert( vec.size() == 2 ); // Doesn't trigger: size is 2. printf( "{3,4}.size() = %d.\n", int( vec.size() ) ); } } auto main() -> int { printVectorIfSpecial( std::vector<int>{3,4} ); } </code> The assert is just about the possibility that without knowing exactly what it does, {3,4} /could/ be invoking the 2-argument vector constructor that would create a vector of 4 items, each of value 3. But given that the code works, it's hard (for me) to say why your original isn't permitted. Cheers, & sorry if that doesn't really help, but, - Alf |
Ben Bacarisse <ben.usenet@bsb.me.uk>: Sep 30 07:11PM +0100 > suppose that I want code which says "do something if the vec is {3, > 4}" Is my code (which works) best practice, or should I avoid casting? > If so, how? With Alf's answer you have the full picture: the direct comparison is not permitted for grammatical reasons, but you can do what you want (in this case at least) by writing the operator== function call directly. A function call's arguments (in C++11) may include a number of brace enclosed initializer clauses, but these are not permitted on either side of an equality operator. -- Ben. |
Gareth Owen <gwowen@gmail.com>: Sep 30 07:21PM +0100 > got the error and was trying to avoid similar problems in future. So, > suppose that I want code which says "do something if the vec is {3, > 4}" Is my code (which works) best practice, or should I avoid casting? I think partly your difficulty lies in thinking of std::vector<int>({3,4}) as a cast. It really isn't, it's actually constructing a whole new object and initialising it with the values. By analogy, your code is more like: if(v == std::vector<int>(19,7)) { // do something if v is a 19 element vector of '7's. } So, you might use a const local at function or file scope namespace{ const std::vector meaningful_name({3,4}); } int myfunc(void) { // or maybe static const std::vector alternative_meaningful_name({3,4}); if(v == meaningful_name){ // ... } } |
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