Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 198 199 [200] 201 202 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887314 times)

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #2985 on: September 28, 2012, 05:19:02 pm »

A header file is for declaration of publicly accessible functions. It essentially provides the interface for your class so that you could distribute it as a binary object and people could still build code that references its methods.

so, if the header file is ONLY for publicly accessible functions, I can't put private class variables in it?  :confused:

Its c++, you can poop anywhere you want, it just makes a mess. You can even put your whole code in the .h if you want to. Or you can not use .h files and just include .cpp.
« Last Edit: September 28, 2012, 05:20:39 pm by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2986 on: September 28, 2012, 05:23:05 pm »

A header file is for declaration of publicly accessible functions. It essentially provides the interface for your class so that you could distribute it as a binary object and people could still build code that references its methods.

so, if the header file is ONLY for publicly accessible functions, I can't put private class variables in it?  :confused:

Its c++, you can poop anywhere you want, it just makes a mess. You can even put your whole code in the .h if you want to. Or you can not use .h files and just include .cpp.

Quote from: Bjarne Stroustrop
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2987 on: September 28, 2012, 05:38:37 pm »

ok, yeah, I need to start doing muli file shit, because right now I have a few 3000+ line projects that are all in one file, and it's starting to get annoying.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2988 on: September 28, 2012, 05:39:46 pm »

Multiple .cpp files confused me for the longest time. My first big project just had 1 .cpp file (for the main function) and everything else was in .h files.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2989 on: September 28, 2012, 06:49:25 pm »

Though some of the things mentioned will cause linker problems in Visual Studio; particularly doing an 'include' for .cpp files. It's why I had everything in .h files except main all the way up until I finally figured it out. By which point I was doing 3D with DirectX. :P

Which is rather problematic, as your programs' include dependencies then have to be a tree structure w/o loops or linker errors appear everywhere. Which is also why I started keeping a folder called 'Teh box of programming lolz.'
« Last Edit: September 28, 2012, 06:52:08 pm by alway »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2990 on: September 28, 2012, 07:30:16 pm »

For me, I think the biggest thing about .h files is the size of the class. C++ allocates memory based on how big it thinks your object will be, and it only knows your .h class. For pointers, this doesn't matter, as they're "pointer-sized".

So in order to make an array of Objects, you must include Object.h where all Objects' members are defined. Functions can be declared but I think that's optional (although I always delcare all functions in the .h anyways). For pointer-members to work, a simple empty declaration at the beginning is enough.

For instance:
Code: [Select]
Mug.h:
//<-- no includes! Usually the only one needed here is the class we're extending from.
class Liquid; // empty class!

class Mug {
public:
  Liquid* contents;

  void drinkContents();
}
------
Mug.cpp:
#include "Mug.h"    //<-- we'll be using stuff declared here
#include "Liquid.h"  //<-- we'll be using stuff declared in here as well

void Mug::drinkContents(){
  if(contents){
    contents->drink();   //<-- and here we call an actual member function.
  }
}

Non-pointer-members are trickier, because their size is added to the size of the container object. Here you should be careful not to create circles, or everything goes to hell.
Moral of the story: .cpp files should include .h files, and .h files should include the least possible number of .h files.
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2991 on: September 28, 2012, 11:51:47 pm »

So today I learned something. DirectCompute is terrible. It's integrated with DirectX's hlsl; that means metric craptons of initialization to pass any data to or from it. Its feature set is pretty much a more limited subset of both CUDA and OpenCL, while simultaneously being harder to use what is there.

It has terrible documentation, resulting in all sorts of little hidden pitfalls. For example, an RWTexture, from which the GPU can read and write, effectively the preferred means of passing in data to DirectCompute, can be set as types like 'float' 'int' 'float4' 'int4' 'uint4' and so on. However, you can't actually read from any types which aren't single values of size 32 bits; which means float or int. You can, however, write to the other types. And due to it being pretty terrible, good third party material is nearly nonexistent. The only real tutorial microsoft provides is in the form of about 3 hours of 20 minute videos. Because everyone loves to listen to hours of people reading documentation rather than having searchable text tutorials with clearly denoted sections and sample code.

In summary, use OpenCL.
Logged

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2992 on: September 29, 2012, 03:31:20 am »

well yeah, OpenGL > DirectX in just about every way.

and Mego, I had a question, can you explain what you meant by

#ifndef FOO_H_INCLUDED // Guard word, used to make sure that this code only gets included once in any compilation target, to prevent multiple definition errors
#define FOO_H_INCLUDED
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2993 on: September 29, 2012, 03:49:41 am »

There's also a #endif later, which is important.

Basically that little block of code prevents multiple declaration errors. The compiler will skip trying to define something twice if it comes across the same header twice. Anything between the #ifndef and the #endif gets skipped if the word is already declared (which is the first thing that happens after the #ifndef).
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2994 on: September 29, 2012, 03:54:50 am »

Some compilers support #pragma once, which I think does the same thing.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2995 on: September 29, 2012, 03:59:58 am »

There's also a #endif later, which is important.

Basically that little block of code prevents multiple declaration errors. The compiler will skip trying to define something twice if it comes across the same header twice. Anything between the #ifndef and the #endif gets skipped if the word is already declared (which is the first thing that happens after the #ifndef).

but i'm guessing the "FOO_H_INCLUDED" gets replaced with something, ya?  ...but what?

I think i'm gonna go the  #pragma once route, and see if my compiler supports it.
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2996 on: September 29, 2012, 04:01:16 am »

Well it just needs to be a unique word. So yeah, replace it with whatever, so long as it won't conflict with any variable definitions or other headers or whatever.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #2997 on: September 29, 2012, 04:03:32 am »

Usually, it's named what your file is named.
So if your header is calculating.h, write
#ifndef CALCULATING_H
#define CALCULATING_H

...

#endif //CALCULATING_H
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2998 on: September 29, 2012, 04:10:50 am »

good to know,
also, when/where should I initialize class objects?
I'm used to initializing them with the class definition.

class myclass
{
     public:
     myclass();

     private:
     int var;
}myObject;

is that bad coding would you say? and it should still work that way, yeah?
also, what are the rules of calling functions from a different source file?
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

kaijyuu

  • Bay Watcher
  • Hrm...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #2999 on: September 29, 2012, 04:19:10 am »

Yeah, that's bad coding since it's making a global variable, which isn't good outside of a few things. Instead, you'll want to declare instances of objects when you need them, similar to how you'd declare ints or whatever. So if you only need to use the object in a single function, just declare it within the function as a local variable. Stuff like that.

Quote
also, what are the rules of calling functions from a different source file?
You'll need to define the function first (generally done in headers). The actual code will be in another .cpp file, but the function's name and arguments would be defined so the compiler knows it actually exists.

So in a hypothetical MyFunction.h header, you'd have something like this:

void MyFunction(int);


And within the MyFuction.cpp file, you'd have this:

void MyFunction(int Value)
{
//DO STUFF
}


And you'd include MyFunction.h in any .cpp file where you want to use the function.
Logged
Quote from: Chesterton
For, in order that men should resist injustice, something more is necessary than that they should think injustice unpleasant. They must think injustice absurd; above all, they must think it startling. They must retain the violence of a virgin astonishment. When the pessimist looks at any infamy, it is to him, after all, only a repetition of the infamy of existence. But the optimist sees injustice as something discordant and unexpected, and it stings him into action.
Pages: 1 ... 198 199 [200] 201 202 ... 796