Вопрос

When I decided to teach myself to program, I started with Java. And although I'm familiar with writing simple procedural software (mostly in PHP), I've recently come to realize that for more complex operations I rely on objects.

My first Java game spawned opponents at random locations and with random speeds by instantiating objects. I am puzzled by the concept of implementing this procedurally in C (which I have very little working knowledge of), but I could only think of creating different arrays for the variables in each opponent and using the array indices as pseudo-object reference 'variables'.

int size[50]; // opponent size
for (i = 0; i < 100; i++) {
    size[i] = random_number();
}

Is there a simple solution that my object-oriented mind has overlooked? Or do certain problems really lend themselves to object-oriented concepts to the extent that you have to make up fake objects (or at least use similar models)? I see structs in C code all the time, and I know they're similar to classes in concept, but that's all I know. So I'm wondering if stuff like this is possible with a totally non object-oriented mindset. There's no real substitute for instantiation, is there? It's all just allocation of memory, so the way I see it, certain C programs have no choice but to be written using some form of what I consider objects.

Please tell me I'm overlooking something, because otherwise I can't respect C purists who berate the object-oriented paradigm. (I'm by no means an OO nut; my mind has become very accustomed to the idea, though, and it's hard to see things through a different lens that doesn't offer what you're used to.)

Thanks!

Это было полезно?

Решение 2

If we neglect all advanced object oriented programming concepts, as inheritance or polymorphism by type parameters everything boils down to the same thing.

In C++ you have objects and you call methods that are attached to them, in C you have structs and you call global functions by passing these functions by argument.

What I mean is that the essential oop paradigm is conceptually to have objects which are able to perform things through their methods. Now in C you can obtain the same behavior but you have functions which are not attached to any object.

So basically in C++ or Java you would have:

class Point {
  private:
    int x, y;
  public:
    Point(int x, int y) : x(x), y(y) { }
    void draw() { ... }
};

In pure C you would have:

struct Point {
  int x, y;
}

struct Point makePoint(int x int y) {
  struct Point p = {.x = x, .y = y};
  return p;
}

void drawPoint(struct Point p) {
  ..
}

But the semantic power of both representation is essentially the same, the syntax is different though (and even the concept behind it). Of course everything changes if we start considering more powerful oop features.

In many circumstances you don't need all this power to represent objects, not everything is meant to be oop neither all problems are best solved with an oop approach. If writing a complex engine for a game in C would be overkill at the same time writing low level functions in Java which operates with the system would be overkill at the same time.

Другие советы

As someone who also started out as an OO programmer, it's also taken me a bit to get used to procedural programming. The biggest thing for me is getting used to working with pointers, and remembering that functions are not managed by an object, and instead act on an "object" which you give them. Anyway, I think something like this might be what you're looking for to help you get a procedural image of what you're trying to do.

struct creature {
    int speed;
    int size;
};

void init_creature(struct creature *c) {
    c->speed = random_number();
    c->size = random_number();
}

int main() {
    struct creature creatures[10];

    for (int i = 0; i < (sizeof(creatures)/sizeof(struct creature)); i++) {
        struct creature c = creatures[i];
        init_creature(&c);
    }
}

A struct is probably what you are looking for here.

A struct can contain any number of named variables (like properties on a class) but can't contain methods (theoretically you could put methods in it via function pointers, but that won't really work the way you want it to).

So representing your creatures in C, you might have a struct like this:

struct creature {
  int x_pos;
  int y_pos;
  int speed;
};

To declare a creature "object" and set some properties, you would do so like this:

   void main() {
      struct creature mycreature;
      mycreature.x_pos = 1;
      mycreature.y_pos = 2;      
   }

The struct creature mycreature syntax is a bit unwieldy, so people often typedef structs so they don't have to say struct creature mycreature and can instead just say creature mycreature:

typedef struct _creature {
  int x_pos;
  int y_pos;
  int speed;
} creature;

A struct can contain other structs, so you might also create a point struct and use that for the location variables instead:

typedef struct _point {
  int x;
  int y;
} point;

typedef struct _creature {
  point location;
  int speed;
} creature;

void main() {
    creature mycreature;
    mycreature.location.x = 1;
    mycreature.location.y = 2;      
}

And now things hopefully start to look a bit more familiar...

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top