possible fundamental error

Started by
12 comments, last by sutek 22 years, 1 month ago
quote:
Calling that function without an instance to operate on is nonsensical.


Yeah, but it could be useful to have static class-functions when you wanna keep e.g. all your vector operations in a Vector-Class.

You coul''d make an overload of Vector::Add() that only takes external parmeters and make it static.


  Vector::Add(Vector &V)//...Add V to (*this vector)const Vector Vector::Add(Vector &V1, Vector &V2)//...Add V1 to V2 and return the result  
Advertisement
FXO, what would make more sense, rather than making a separate class for operations would be to have them in the vector class itself, which would save you some considering they would have one of the parameters implied, and it makes polymorphism easier to conrol for different sub-vector classes.

Sutek --
I think the big problem with your class cFight isn''t your syntax. While you can use classes in the way that you are attempting, you seem to be missing the actual concept of what a class is. When you make an instance of a class, it should conceptually be as an object, like their names suggest. Those methods have operations that affect their datamembers and interaction with other classes/global data. Having a class devoted to fighting doesn''t really make much sense, as you''d have to pass in 2 monster parameters, etc. and because many of the methods would have to be static, you''re pretty much using the class as a grouping of functions, which really serves no purpose, and making instances of that class would be pointless.

A better approach would be to have an abstract base class devoted to a generic monster which contains prototypes and pure virtual methods devoted to "attack" which would take one parameter (the object it would be attacking), "defend" (which would put the monster into defend mode for one turn, move (which would move the monster), etc. Each TYPE of monster would be a class derived from that ABC, and everytime you wanted to make one of those enemies, you''d make an instance of that class. With polymorphism you can redefine the attack, defend, move, etc. functions on each derived member in the hierarchy for the associated monster. Also, since they share a common ABC, you can store them all in like terms via a pointer/array of pointers/linked list of pointers etc. to an instance of an ABC, and be able to manage all of your monsters as though they were one type, but still be able to have their virtual methods each uniquely defined. If you are ever unsure of what to use as a class, just think "does this make sense conceptually as an object?" Fighting is more of an action with varying members, so it would better be left as a method in a more general class, IE the moster class as I mentioned above. Object-Oriented programming is a lot like how you''d percieve the world as items, organisms, etc. If you can percieve it as an object in real life, then it probably makes sense as an object in C++. Otherwise, with many exceptions, you should probably re-evaluate the situation.
( I was agreeing with FXO there in the first line, but i think my wording was a little messy)
quote:
rather than making a separate class for operations would be to have them in the vector class itself,


That's what I was trying to say

btw. Is there anyway to get notified when somebody post on a thread that you didn't start?

(Something similar to the
[X] Check here to be notified by email whenever someone replies to your topic - button)

[edited by - FXO on March 16, 2002 8:01:10 AM]

This topic is closed to new replies.

Advertisement