Jump to content
  • Advertisement
Sign in to follow this  
jakovn

Program organisation, classes...

This topic is 3637 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am starting a rather large project in VC++ and since I come from VB background I would like to ask for something before I mess it up. 1. Is it wise to put every portion of the code in separate classes since many objects will have one instance in the programm? 2. How do I declare global veriables. I see in c++ everything is organised in classes and variables I declare in one class does not exist in another. Some functions would need to pass 20 variables from one class to another and I thought I would be better to be accessable on public level. 3. If I have large array, like bitmap pixels, is it much slower to pass it as whole instead using pointers? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
1. Is it wise to put every portion of the code in separate classes since many objects will have one instance in the programm?

I personally recommend it. If there is only going to be a single instance, consider using singletons. However, in most cases, limiting objects to a single instance is not a good idea and is almost never needed. Thus, I recommend not limiting your objects to a single instance unless it is absolutely necessary.

Quote:
2. How do I declare global veriables. I see in c++ everything is organised in classes and variables I declare in one class does not exist in another. Some functions would need to pass 20 variables from one class to another and I thought I would be better to be accessable on public level.


Just define it inside of a source file outside of any classes or functions:
int _globalVar=0;
In the header file declare it extern:
extern int _globalVar;
Now any file that includes that header file can use _globalVar.

On a design note, if you need to pass 20 variables to a function consider if there is any cohesion between them. If there is, it may be more wise to split the code into smaller function units or classes. I havn't seen any case that actually required any globals.

If you need to use a global, you can always declare it as a static data member of an object. In most cases this is not needed though.

Quote:
3. If I have large array, like bitmap pixels, is it much slower to pass it as whole instead using pointers?

Yes. Within C++ you can also use references instead if you do not need the functionality of pointers. ie, "pass by reference".

Share this post


Link to post
Share on other sites
Quote:
Original post by jakovn
I am starting a rather large project in VC++ and since I come from VB background I would like to ask for something before I mess it up.
You will mess it up. Everyone does. Your objective should not be to get a good design up from the start. Instead, your objective should be to keep your code as easily modifiable as possible, so you can change it when you inevitably notice that your design is bad.

Quote:
1. Is it wise to put every portion of the code in separate classes since many objects will have one instance in the programm?
No, it is not wise. Some code does not rely on state at all (consider, for instance, a square root function) and does not belong in a class.

Classes are used to create instances. Instances are used to represent state : the member variables represent the current state internally, and the member functions allow access to some aspects of the current state and transition to another state. Before you can create a class, you must define what the state it represents should be.

Quote:
2. How do I declare global veriables. I see in c++ everything is organised in classes and variables I declare in one class does not exist in another. Some functions would need to pass 20 variables from one class to another and I thought I would be better to be accessable on public level.
There's a high probability that those 20 variables are, as Crypter mentioned, part of the state of an object. Represent that object with a class.

Quote:
3. If I have large array, like bitmap pixels, is it much slower to pass it as whole instead using pointers?
Usually, yes. Note that passing it as references is either faster or safer.

Quote:
Original post by Crypter
If there is only going to be a single instance, consider using singletons.
My goodness. If creating more than one instance will wreak unbelievable havoc upon your program and there is no possible way of working around this issue without spending a few months working on it, then make it a singleton. Otherwise, don't spend the additional effort and just keep a normal class.



Share this post


Link to post
Share on other sites
Quote:
Original post by jakovn

3. If I have large array, like bitmap pixels, is it much slower to pass it as whole instead using pointers?

Thanks


In general passing large classes (those with a large number of instance variables) is slower and should be passed by constant reference. Arrays already "decay" to pointers when you pass them around as such need no extra pointer.

void somefunc(int* A){}
will work perfectly fine when called with:

int Ar[20];
....
somefunc(Ar);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!