Advertisement

Classes, Structs, and Functions Oh my!

Started by June 04, 2002 06:02 PM
4 comments, last by Antion 22 years, 5 months ago
Ok, I am having a little problem here. I am making a fairly simple game, trying to utilize many of the concepts that I have learned in my study of C++. I am now having a bit of a problem. I have an abstract class called Character. In this class it has an attributes structure. Class Character { private: struct attributes { int str; int int; ... } *stats; } from there, I inheriate Character into Player. Now this may be to much information, or too little... anyhow. Here is my problem. I am trying to check the boundries of the arena that I generate. So I am trying to send a reference of the stats struct to a method I created in another class called Map. The method invoked is called Boundry. This is also going to need to have an NPC object access it for boundry checks as well. template bool Map::Boundry( T *actor ) { ... } Now, this is my thing. It is a struct within a class that I am trying to send, so making my template look for a class I feel is wrong (My compiler is telling me the same thing). The other thing I was thinking I could do, is make the template nametype, but then that doesn''t resolve the fact that the struct belongs to a class. I think I am just plain confused! Can someone help me out?!? (That is if anyone can understand this bloody post! I''m not actually sure how clear I am *sigh* ) Thanks, Antion
template<typename T> is strictly equivalent to template<class T>

What parameter are you really passing to Boundry (which should be spelt Boundary ) ?

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
In the previous reply, I guess it didn't print out what I had typed. I had "template< class T >"... not just template. That is suppose to be class T between the <> Can't get it to stick... *sigh*

Well, that is kind of the part I am unsure of. Currently I have if( false == ( boundry( &stat ) )
{
...
}


Now you say "template is strictly equivalent to template", and i'm not quite sure what you mean by this.

Oh, as for boundary.... eh, never was good at spelling
Thanks,
Antion




[edited by - Antion on June 4, 2002 7:34:16 PM]

[edited by - Antion on June 4, 2002 7:35:04 PM]

[edited by - Antion on June 4, 2002 7:35:34 PM]

[edited by - Antion on June 4, 2002 7:36:48 PM]

[edited by - Antion on June 4, 2002 7:47:50 PM]
Hmmm, good point, it is declared Private. My understanding was that you should make variables private in order to retain encapsulation. Now, I may be mistaken, but I thought that I could send a pointer (it probably should be const) from within the public of Player(derived from Character) that would send a reference to stats.

That is why I created struct Attribute{ ... } *stats;

I am guessing now that this is wrong? I''m still trying to get this whole class/object thing... so I am not sure if I am creating very good classes yet. Eh, the only way I will learn is practice...right?!?

Thanks,
Antion



Rather than a pointer, pass a const reference.
That way, you make sure that the data cannot be changed directly.
Additionally, rather than templatize your function, you could have it use a public typedef from your character class (I think that''s what your question was).

Try something like

  class Character{public:  typedef struct  {    ...  } attributes_t;};bool Map::Boundary( Character::attributes_t& stats ) {...};...  


Though Map shouldn''t rely on specific types from Character, rather the other way round : Character should pass to Map just the data it needs to compute the boundary check, not it''s whole internal representation.

As for typename vs class : within a template parameter declaration, the two keywords are equivalent.

Hint: post your code between [‍source] and [/‍source] tags

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
That answers my question! Thanks!

This topic is closed to new replies.

Advertisement