View Single Post
Old 10 October 2007, 12:00   #8
eLowar
Citizen of Elthesh
 
eLowar's Avatar
 
Join Date: Sep 2003
Location: UK
Posts: 949
Quote:
Originally Posted by Doc Mindie View Post
Could I here take the line
Code:
#define KILOS_PER_POUND .45359
and stuff it into "conversion.h" then us "#include conversion.h" in the program instead?

(still trying to get me head around the .h system)
Yeah, that would be a common use. Headers are usually just collections of function prototypes and constants like that, which share some properties. For example "stdio.h" is standard IO related stuff.

Note the difference between a constant variable like const double kilos_per_pound .45359 and a pre-processor macro like #define KILOS_PER_POUND .45359. The difference is that the former is an actual variable (albeit one that can't be modified) and the latter simply performs text substitution before compilation. For example with GCC you can use the -E parameter to see what your files look like after the preprocessor is done with them (including files, replacing #defined stuff, etc.).

Quote:
Originally Posted by Doc Mindie View Post
If we use AmigaE terms, (snippeted from the source to module Devices/cd.m)
Code:
OBJECT cdinfo
  ...
ENDOBJECT
makes much more sense to me..... Maybe it's because it's an "amiga standard" or maybe it's because I started to get a grasp with AmigaE some 10-12 years back.... but the .m's of AmigaE makes more sense than C's .h
I don't know much about E, so I don't know if that's an actual class (in the sense used in C++ and Java) or just a collection of data. If the latter, you get something similar in C in the form of structs.

The rough equivalent of the E code you posted would then be:

Code:
struct cdinfo {
  unsigned int playspeed;
  unsigned int readspeed;
  unsigned int readxlspeed;
  unsigned int sectorsize;
  unsigned int xlecc;
  unsigned int ejectreset;
  unsigned int reserved1[4];
  unsigned int maxspeed;
  unsigned int audioprecision;
  unsigned int status;
  unsigned int reserved2[4];
};
Following that you can declare and use variables like this:

Code:
struct cdinfo myCdinfo;
...
myCdinfo.playspeed = 5;
You'll definitely want to read up on the subtleties of this, but that's the basic idea. In C++ there are actual classes with inheritance, visibility modifiers and member functions. Like this:

Code:
class MyClass: public SomeOtherClass {
public:
  MyClass(): SomeOtherClass() { /* inline function code */ }
  ~MyClass(); // this function and the following are defined elsewhere
  virtual void foo();
  int bar(char* parameter);

private:
  int privateValue_;
};
That declares a class MyClass which inherits (in a certain way, hence the public, I'll skip the details for now) all properties of the class SomeOtherClass (that is, it'll contain all the functions of the other class, etc.). The function MyClass() is called when you create an instance of the class (create a variable of that type), ~MyClass() is called when an instance is destroyed (e.g. by going out of scope). privateValue_ is private and hence can only be accessed from functions inside the class, not from the outside. The virtual keyword changes how the program looks for the right function to execute in inherited classes. Again, I'll skip the details, just trying to give you an overview and maybe see if you recognize any of this from E).

Note that in C++, a struct is really just a class in which everything is public (more or less ).

Anyways, if E is an object oriented language and you've worked with it, you might feel more at home with C++ than C.

Last edited by eLowar; 10 October 2007 at 13:21. Reason: Corrected minor syntax error and added some comments.
eLowar is offline  
 
Page generated in 0.04643 seconds with 11 queries