Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Is this weird ?
 Post Posted: November 2nd, 2017, 2:33 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
I found myself typecasting screenWidth / screenHeight variables so i'd thought i'd do this
Code:
template<typename T>
   static T ScreenHeight()
   {
      return (T)m_screenHeight;
   };
template<typename T>
   static T ScreenWidth()
   {
      return (T)m_screenWidth;
   };


i guess going float height = Locator::ScreenHeight<float>() apposed to (float)Locator::ScreenHeight()
isn't all that different. Think i'll use it though

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
 Post subject: Re: Is this weird ?
 Post Posted: November 2nd, 2017, 4:30 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3538
Location: Japan
It's a little weird, yeah. I don't see it as particularly harmful though.

_________________
Chili


Top 
 Profile  
Reply with quote  
 Post subject: Re: Is this weird ?
 Post Posted: November 2nd, 2017, 8:07 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
Have you looked into variable templates?

template<typename T> T screenHeight;

Let's say your screen res doesn't change.
template<typename T>constexpr T screenHeight = static_cast<T>( 800 );
template<typename T>constexpr T screenWidth = static_cast<T>( 600 );

Then you can use them like so:
cout << screenHeight<float> << endl; // Prints screenHeight as a float
cout << screenHeight<int> << endl; // Prints screenHeight as an int

_________________
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: Is this weird ?
 Post Posted: November 2nd, 2017, 8:14 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2970
Location: Oklahoma, United States
As class members though, I think they need to be static, but you seem to be doing that anyway. If they do change however, then this wouldn't be a good fit. Changing the float version seems to create a new instance and doesn't change the int version for example.

Code:
   {
      screenHeight<float> = 1024.f;
      cout << screenHeight<float> << endl;   // Prints 1024
      cout << screenHeight<int> << endl;      // Prints 800
   }

   {
      cout << screenHeight<float> << endl;    // Prints 1024
      cout << screenHeight<int> << endl;       // Prints 800
   }


So it's not a scoped change either.

_________________
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: Is this weird ?
 Post Posted: November 3rd, 2017, 1:31 am 
 

Joined: November 30th, 2013, 7:40 pm
Posts: 656
Location: Merville, British Columbia Canada
@albinopapa .. Interesting stuff, I haven't done variable templates like that before

_________________
Curiosity killed the cat, satisfaction brought him back


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 5 posts ] 

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: