Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++]get/set versus public fields

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Thu Jan 06, 2011 4:34 am    Post subject: [C++]get/set versus public fields Reply with quote

I don't really have much need to check the input at all, just passing a class instance by reference, that's the extent of the work being done... at the moment, at least. Is it generally preferable to wrap things like this in getter / setters?


Also on the subject of style:

createBuffer(BUFFERTYPE type, Buffer& b);

vs

createVertexBuffer(...)
createIndexBuffer(...)

They're both just simple chunks of memory at their core, whatever information needed about them, like number of verticies, can be figured out easily anyway. It would be condensing two almost identical implementations down to one.
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Thu Jan 06, 2011 6:26 am    Post subject: Reply with quote

If this is for yourself, I would say go with solution 1 and document it well in code. But as far as standards are concerned, solution 2 is "best". I've talked with many people in industry who say that they have these ginourmous handbooks about readability of code, and that the code should usually speak for itself if written correctly. I think it's obvious that solution 2 is much clearer than 1.
_________________
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Thu Jan 06, 2011 9:02 am    Post subject: Reply with quote

If you're not wanting to do anything else in the getter/setters then use public. You should use get/set instead of public when changing some property implicitly changes another and requires its update. At least, this is the rule I follow. However at the end, it is down to preference and no one is likely to berate you for using one over the other.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Thu Jan 06, 2011 8:39 pm    Post subject: Reply with quote

#2 is my personal liking. Will look better as #2 vs. having #1 which will turn into some sort of if/else or switch case.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Fri Jan 07, 2011 7:24 am    Post subject: Reply with quote

Wiccaan wrote:
#2 is my personal liking. Will look better as #2 vs. having #1 which will turn into some sort of if/else or switch case.


That's what it would boil down to internally, but I see why it's silly.


I guess it would have worked something like this, going the second route
(typed here so formating is hosed)

Code:
struct BUFFERDESC
{
BUFFERTYPE type;
u32 length;
};

createBuffer(BUFFERDESC& bd, Buffer& b);


In trying to make something less verbose I made it more so. Having to fill out a struct (simple, but nevertheless) for something like that doesn't seem to make much sense, maybe if there was more to track about what makes up the buffer, but there isn't.


It seemed more sensible last night before I went to sleep... :\
adding to the pile of sleep deprived brilliance.
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Fri Jan 07, 2011 10:56 pm    Post subject: Reply with quote

Never make variables in a class public. Ever. This is exposing the implementation of a class, when all you want to expose is the public interface that describes the behavior of the class.

It breaks:
  • Class invariants, if they exist.
  • Polymorphism. You don't have the ability to extend the class later.

Furthermore, if you find you're making a lot of getters/setters you're probably doing something wrong. Classes represent objects and methods are a way of interacting with an object of that type (think 'actions') - getting and setting private variables breaks this line of thinking (it also destroys any notion of abstraction).

With regards to the second question, present #2 to the users and use #1 internally - you get the best of both worlds.
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Jan 08, 2011 4:56 am    Post subject: Reply with quote

I don't really have a lot of getters / setters, but hopefully I have somewhat of a grip design wise.

Right now I have a structure like:


The engine holds and manages the various interfaces. Being primarily a graphics engine though, the rendering device is the beefiest thing by far.

The renderer manages and creates just about everything drawing wise... like surfaces to draw on, which in turn, wrap basic pixel drawing stuff and various types of blits so you don't have to drop down to the actual block of memory yourself if you don't really have to. So to do 2d drawing, there's an aptly named getBackbuffer() function that returns a pointer to the primary surface. Not entirely sure if I like having a surface do its own blits (to another surface even), but surfaces can also load bitmaps, so drawing a picture is simply having it load a file then copy it to the backbuffer.

The renderer doesn't really implement much itself, just wraps most stuff up. Drawing 3d it just pulls data from the buffers and matricies it holds and other objects get to nom on that and do their own lifting, like the vector type implements the transformation to screen space, etc.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites