POV-Ray : Newsgroups : povray.off-topic : C++ structuring help.... : Re: C++ structuring help.... Server Time
10 Oct 2024 17:18:20 EDT (-0400)
  Re: C++ structuring help....  
From: John VanSickle
Date: 21 Mar 2008 16:56:21
Message: <47e42f05@news.povray.org>
stbenge wrote:
> Hi,
> 
> As I progress in my understanding of C++, I come closer to the 
> inevitable conclusion that in order to make my code more efficient and 
> easy to alter, I need to learn more about classes and structs. Simple 
> concepts, but the problem comes with implementing said features.
> 
> A simple (tile based) level editor is my current project. Already, I 
> have about two dozen global variables. It's getting messy, and I'm 
> having to rewrite code to handle, say, foreground tile editing as 
> opposed to background tile editing.
> 
> Does anyone have a link to some good tutorials to help me manage this 
> ever-growing pile of data? I need to encapsulate everything. I'd like to 
> rid myself of global variables entirely, if that's possible. Thanks~

The easiest way to get rid of the globals is to declare a class which 
has some static members in it:

appdata.h:

class AppData {
     static int AppDataInt;
};

appdata.cpp:

#include "appdata.h"
int AppData::AppDataInt=0; // or whatever

Everytime you want to use your formerly global data, you include 
appdata.h in the file and make sure you use the scope resolution to 
access it.

One benefit of this is that you can now borrow code from another project 
that uses globals without worrying about those globals conflicting with 
your ex-globals.

Additionally, you can make make the static members of AppData private 
members, and access them through member functions of AppData, to enforce 
sanity checks on the changes you make to the data.

I've been doing C++ since '02, and most of the hands-on involves my 
modeling application; it was originally written in C starting in 1999, 
and much of the code base remains a product of non-OOP C thinking.

Regards,
John


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.