Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hey Albinopapa
 Post Posted: May 31st, 2015, 2:09 pm 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3448
Location: Japan
What's up?

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 1st, 2015, 5:50 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Uh, weird being called out like this...what's up bro? lol.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 1st, 2015, 12:52 pm 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3448
Location: Japan
Not much man. I've just gotten over an obsession with indoor rock climbing and semi-got over Texas Hold'em (still a little into that one), so now I'm looking to up my programming time per week.

Just fooling around with HUGS stuff this weekend. C++ giving me a goddamn hard time. By the way, I have a riddle for you.

You know how much of a hard-on I have for const-correctness right? (rock solid) What do you think is a good reason NOT to use const for data members of a class?

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 1st, 2015, 6:15 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Hmm, getting over an obsession of indoor rock climbing, sounds like there was a particular reason for getting into it in the first place...someone perhaps? hehe. Hold'em 'ay? Didn't like it at first, but the more I play the more I enjoy it. Still can't play for hours though. Glad to hear you are doing well and getting back to the giving us some HUGS.

As to your riddle, I've been trying const here and there for function params, but only have I ever consted data members is if they will never change. Have found one caveat to that however. Using the move semantics you end up having to cast off the const when using the move assignment operator (operator=( Object &&)).

Hmm, just thinking out loud here. The only data member types I would const would be,
min/max values, math constants such as gravity or speed of light, data that define a physical object like a model's vertices or it's size. At the moment I can't think of much more to const.

If you are looking for blanket reason NOT to const any data members, I would have to say when you are going to be moving the class when not using the constructor, as in the move assignment operator. The best example I can think of would be an array of objects can't be initialized except through the default constructor, but if you need the data to be different than default, you have to use the = operator, which would create the need for const_cast or whatever.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 2nd, 2015, 6:30 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
So what's the answer? Or, just looking for a different outlook.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 3rd, 2015, 2:20 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3448
Location: Japan
You pretty much nailed it papa. Constant data members (i.e. constant embedded objects, pointers and refs to const are fine) cause problems when you wanna make copy/move assignment operators. Universal constants (ones that are constant across all instances of a class) are fine because you just ignore them in the assignment operator (they should by definition already be the value that you would have assigned them). But object lifetime constants (which vary from instance to instance but do not change during the lifetime of any one instance) are problematic.

Generally speaking, when I have a class (like say a Ship class), I don't generally use the assignment operator because it doesn't make much sense. Each ship is its own contained entity; it makes little sense to take the attributes of one and then overwrite another with them. However, that is exactly what will happen when you try and stick you ships in an STL container (specifically, a vector) and then sort them. The algorithm will use assignment as it's swapping them around, and you'll be f'ed in the a.

The idiom of making variable that do not change over the lifetime of an object const makes a lot of sense to me. Consider the case of a class that contains a unique ID member so that each instance is uniquely identifiable. You will likely have a function or operator that compares the uids of two ships to test if they are the same ship or not. A common typo is: if( uid = otherUid ). Try that with uid as a const member, and you get a compile-time error pointing you to the exact spot where you fucked up. Without const, you could be pulling your hair out for an hour or more trying to figure out why shit's not working.

That being said, I think I'm going to have to give up on consting the fuck out of class members, because I'm not trying to give up compatibility with the STL here just to satisfy my sensibilities.
One solution would be to use containers of unique_ptr which then hold objects dynamically allocated. That would however be a lot slower. Can't justify sacrificing what, in certain situations, would be a lot of performance for my const fetish either.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 3rd, 2015, 4:27 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
I would use a vector of pointers, ship addresses, in that particular case.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 3rd, 2015, 6:02 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3448
Location: Japan
chili wrote:
One solution would be to use containers of unique_ptr which then hold objects dynamically allocated. That would however be a lot slower. Can't justify sacrificing what, in certain situations, would be a lot of performance for my const fetish either.


http://www.bfilipek.com/2014/05/vector- ... nters.html

The difference between a naked pointer and a unique_ptr (or even a shared_ptr for that matter, from the perspective of iteration over the collection) is negligible.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 3rd, 2015, 7:46 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2782
Location: Oklahoma, United States
Yeah, speed vs ease of programming, it's a tough one. Thanks for posting the article, didn't realize that using a vector of pointers would be that much slower than to use just a vector of objects.

_________________
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com


Top 
 Profile  
Reply with quote  
 Post subject: Re: Hey Albinopapa
 Post Posted: June 3rd, 2015, 9:21 pm 
User avatar

Joined: October 1st, 2012, 9:53 pm
Posts: 326
Location: Canada
Literally walking out the door to go indoor rock climbing and I quickly check the forums and read this... Am I dreaming? Is this real life?


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron