Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / C++ Coding Questions Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
Ron Jeremy

March 22, 1999, 06:14 PM

Ater my map file format and what I wanted to do with it began to really get out of control, I made the brief conversion from C to C++. Since I'm new to OOP, I'd like to know if there's any common mistakes that are made from changing programming styles to C++ from standard C, so I don't make them myself before it's too late. I also have a few questions :

- Is the new (whatever) function some sort of calloc for classes?

- Does anyone ever use those things?

- When using classes, which do you recommend :
1. Make the properties of it (such as flags, alpha, etc) protected and use scopes, i.e. this->setx(int x)
2. Or should I make them public access them directly, like this->x = int x?

I apologize for these horribly beginner questions in a forum meant for people who write self-modifying spaghetti code directly to their 3D cards and have P.h.Ds in advanced mathematics :), but anyone kind enough to spend a few minutes of their time to help an ignorant 16 year old would be quite appreciated.

-The Ronster

 
Jaap Suter

March 22, 1999, 06:36 PM

Howdy!!!!

----Is the new (whatever) function some sort of calloc for classes?

Yes it is. Just remember that every new() has a delete() too (or terrible things will happen
like cool memory leaks!).
I never use them. I declare everything before compile time. In my
programmes there is never dynamic memory allocation (is this a bad thing?)

------- Does anyone ever use those things?

These are just when you, for example, want to define a class for vector's that can use both
integers or floats for X, Y and Z. I personally never use them however cause i like to keep
my code clean! Besides floats are speedy enough nowadays, and I cannot think of a situation
where templates might come in handy (in the 3dengine expertise i mean!)

--- When using classes, which do you recommend :
Always make all you class variables private (protected means that derived classes still
have acces, private is even safer!). Public should only and only be used for class methods.
Sometimes i use protected but in 99 percent i use private variables.

I then just write functions like getX() and setX(). Even if the variable is fully modifiable i
still keep it private. It forces clean coding and is much safer. Besides if you make sure all
your acces modifiers (like getX and setX (this kind of functions are called acces modifiers) )
are declared INLINE then there is no performance loss at all.

----- apologize for these horribly beginner questions in a forum meant for people who write

No need for this. I love to do something back for the game coding community. I still get and
need lots of help from people all over the internet. This is the only way programming games
will live up to it's standard.

I am not a coding expert at all though so don't take my advice as a law, however.
These are just the ways I do it. When something is still not clear dont hesitate to email me!

Anyways: code clean, play goldenEye and go vegetarian!

seeya!

 
Ron Jeremy

March 22, 1999, 07:26 PM

Wow, that was a quick response =)

Jaap,

Since you're so kind as to help out people like me too poor to buy a book on this stuff, another thing about new() function I'd like to know is how to make new pointers to pointers, i.e. more than one, and making arrays of classes. FYI, I'm writing a 2d engine, since this is my first game, and the main issue of it is tile storage. I'm storing the tiles as

tile_c **tiles; // tiles[x][y] inside a class describing the parallax layer

Using classes in C++, how would I say :

tiles = (tile_c **)malloc(xsize * sizeof(tile_c *));

for(...)
tiles[x] = (tile_c *)calloc(ysize, sizeof(tile_c));

I unfortuately don't have any C++ docs or source code that do this, so I don't know how to do these...

The goal of my 2d engine is to make everything as dynamic as possible, using any number of parallax layers as large as you want, with any properties that you want, so I'm not restricted when building levels (plus I like a good challenge over quick, sloppy code). I'm aiming for a 2d engine that's about Castlevania:SOTN quality (except in SVGA!), with some possible 3d/demo backgrounds and such.

>>------- Does anyone ever use those things?
>>
>>These are just when you, for example, want to define a class for vector's that can use both
>>integers or floats for X, Y and Z. I personally never use them however cause i like to keep
>>my code clean! Besides floats are speedy enough nowadays, and I cannot think of a situation
>>where templates might come in handy (in the 3dengine expertise i mean!)
>>

Thanks for explaining that, the C++ tutorial I have is too abstract to simply say that.

>>--- When using classes, which do you recommend :
>>Always make all you class variables private (protected means that derived classes still
>>have acces, private is even safer!). Public should only and only be used for class methods.
>>Sometimes i use protected but in 99 percent i use private variables.
>>
>>I then just write functions like getX() and setX(). Even if the variable is fully modifiable i
>>still keep it private. It forces clean coding and is much safer. Besides if you make sure all
>>your acces modifiers (like getX and setX (this kind of functions are called acces modifiers) )
>>are declared INLINE then there is no performance loss at all.
>>

Okay, I see your point now... You're right, it is a lot cleaner that way. Sloppy code is the reason I'm rewriting my map stuff for the THIRD time :), but at least I'm learning from it.

Another question I'm uncertain about is : what exactly do __INLINE__s do, anyways? I know they're supposed to be faster for returning, or something. And what exactly does #define code do, does it insert code repeatedly when compiled, as opposed to jmp'ing to code addresses?

>>----- apologize for these horribly beginner questions in a forum meant for people who write
>>
>>No need for this. I love to do something back for the game coding community. I still get and
>>need lots of help from people all over the internet. This is the only way programming games
>>will live up to it's standard.
>>
>>I am not a coding expert at all though so don't take my advice as a law, however.
>>These are just the ways I do it. When something is still not clear dont hesitate to email me!
>>
>>Anyways: code clean, play goldenEye and go vegetarian!
>>
>>seeya!

Thanks a lot for the help, that's a good attitude to keep... I appreciate it. I didn't have any trouble learning from you, you couldn't have explained it better.

However, once you see my first game (I take my time, it could be months upon months from now), you may be sorry you ever helped me, my intention is basically to create a new standard (and I mean that) for videogame violence and shock :)

Bye.

-Ron "The Hedgehog" Jeremy

 
David Smith

March 23, 1999, 06:19 AM

Heres how you construct your tile array. I tried to be complete.

class tile_c
{
};


int main()
{
int array_length = 100;
int i;
tile_c **tiles;

tiles = new tile_c*[array_length];

for( i = 0; i < array_length; i ++ )
{
tiles = new tile_c; } return 0; } >>>>------- Does anyone ever use those