Advertisement

Question about VC++ programmuing

Started by May 07, 2002 01:11 PM
3 comments, last by Corrail 22 years, 9 months ago
Hi all! I''ve a problem with VC++ programming. I want to create a class named triangle with a public void render() and a class named cube with also a public void render(). So if I want to draw a shape (triangle or cube) I don''t know what class this shape is, how can I code this? I tried this, but this doesn''t work: class test1 { render() { //Rendering } } class test2 { render() { //Rendering } } void main() { //Code test1 var1; test2 var2; void *pointer; pointer = &test1 pointer->render(); //Code } Can anyone help me?? Thanks a lot, Corrail
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
This is what C++ is all about! C++ has a number of facilities to allow you to do this, such as inheritance and virtual functions. I would suggest you create a class Shape (or whatever you like to call it) with a virtual function called Show(), and let the other shapes inherit from the base class Shape.
    class Shape{   public:      virtual void Show(void) = 0;}; class Triangle : public Shape{   public:      void Show(void) {}}; class Rectangle : public Shape{   public:      void Show(void) {}}; void main(void){   Triangle t;   Rectangle r;   Shape* s = &t   s->Show();   s = &r   s->Show();}      
This probably doesn't explain very much, but it should be a (the?) solution. Look up some articles on inheritance and virtual functions!

[edited by - Miserable on May 7, 2002 2:22:56 PM]
Advertisement
Mind the semicolons after the class declarations

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Thanks!!
This is exactly what I''m looking for!! :DD
I''ve already tried it with virtual voids but I used them in a wrong way! :D

Thanks a lot!!
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
This is easily the most powerful feature of C++, with this, all sorts of insanly cool stuff is surprisingly easily possible. you just have to think latterally as to how to go about it.

A good example:

Quake 2 code vs half-life.

in HL, the way it goes about things is that all entites are derived from various basic objects (strarting from the simplest entity)... so all objects can therefor be kept in a list of the base object pointers... hence they can all be called to render, or whatnot, in a loop.

In Q2, however, as far as I can tell, every object is recoded from scratch. you look through, and there are duplicate functions everywhere doing the same thing, but just for a different enemy type. Now, how many lists of objects are needed when each has to be different? Lots. It''s hard enough debugging a single object, but what about 30 or so? especially if they are all similar. What do you do if they all have 1 bug, that varies slightly from object to object? you have to debug all 30. with inherited objects, you debug it once to effect all objects.

a personal example: (I can''t stress enough how powerful this technique is, it''s saved the app several times)

And, also, this is very important,
This is what I did in my car physics app, every object is inherited from a base object, that can link to other objects, have forces applied to it, etc.
The bouncing sphere simply adds a function called DoCollisionDetection(). this is called within Active(), which is called by the app 70 times/frame automatically.
Now.
thats all the code for that object. some 50 lines or so.
move on to the wheel.
all it replaces is ApplyForce(...) where it works out what is appropriate to do. it''s the same for suspension.
This means, if a bug effects all objects, it''s in the base object. if it effects the wheel, I have 15 of lines of code to look through.
This makes making powerful stable apps much easier.

http://www.riptorn.8m.com

This topic is closed to new replies.

Advertisement