Skip to main content
gamedev.net
🔒 Locked

Parameter types

Started by nGamer 6 replies 1.3k views
fastcall22
fastcall22
Quote:
Original post by nGamer
If I have a function

void some_function (std::vector ),

Is there anyway that I can have the function allow vectors containing all objects deriving from A be passed into this function?


What do you mean by "allow"? The current function declaration does just that: The function takes a vector (or rather, copies the vector*) of pointer-to-A-like-objects.

Are you asking whether or not you can cast a std::vector to a std::vector?

*Did you mean to pass the vector in by reference?:
void some_function( std::vector<A*>& avec );
nGamer
nGamer
As it were, I have a function,
void test_entities_outOfFrame (std::vector <Entity *> *e, int width, int height){  for (unsigned int index = 0; index < e->size ();)  {    if ((*e)[index]->get_positionX () < 0) (*e)[index]->set_positionX (width) ;    if ((*e)[index]->get_positionX () > width) (*e)[index]->set_positionX (0) ;    if ((*e)[index]->get_positionY () < 0) (*e)[index]->set_positionY (height) ;    if ((*e)[index]->get_positionY () > height) (*e)[index]->set_positionY (0) ;    ++ index ;  }}

that checks the object's position and set the objects' position back to the visible playing field if it is off-screen. But you see, I have vectors containing types of Entity that I would like to pass in.
visitor
visitor
Are templates out of the question?

template <class T>void test_entities_outOfFrame (std::vector <T*> *e, int width, int height){  for (unsigned int index = 0; index < e->size ();)  {    if ((*e)[index]->get_positionX () < 0) (*e)[index]->set_positionX (width) ;    if ((*e)[index]->get_positionX () > width) (*e)[index]->set_positionX (0) ;    if ((*e)[index]->get_positionY () < 0) (*e)[index]->set_positionY (height) ;    if ((*e)[index]->get_positionY () > height) (*e)[index]->set_positionY (0) ;    ++ index ;  }}


Now vector of any pointer having those methods would go.
Shinkage
Shinkage
If I read you right, what you want is a template function:

template<typename TElement>void some_function(std::vector<TElement> &vec){   for(unsigned int i=0; i<vec.size(); ++i) {      // Function will give a compile error here if TElement doesn't      // derive from Entity.      Entity *e = vec;      // ... do stuff with e ...   }}
rip-off
rip-off
If class Derived inherits from class Base, this does not imply that vector can be passed to std::vector, by value or pointer or reference.

You can however write template functions:
// Don't pass std::vector by pointer if you can avoid it!template<typename T>void test_entities_outOfFrame (std::vector <T *> &e, int width, int height){  for (unsigned int index = 0; index < e.size () ; ++ index)  {    // Moving common sub-expressions into a named variable    // Can make your code easier to read    T &instance = e[index];    if (instance.get_positionX () < 0) instance.set_positionX (width) ;    if (instance.get_positionX () > width) instance.set_positionX (0) ;    if (instance.get_positionY () < 0) instance.set_positionY (height) ;    if (instance.get_positionY () > height) instance.set_positionY (0) ;    // don't increment here if you can do it in the for() statement  }}


[edit: Shrinkage's suggestion is a good one: unless you want compile-time duck typing]
nGamer
nGamer
Quote:
Original post by _fastcall
Quote:
Original post by nGamer
If I have a function

void some_function (std::vector ),

Is there anyway that I can have the function allow vectors containing all objects deriving from A be passed into this function?


Are you asking whether or not you can cast a std::vector to a std::vector?


That is what I meant. I apologize; when I typed in the less than symbol preceded by the capital 'A', the asterisk, and then the greater than symbol, my text was underlined in the post. How do I fix that?
Zahlman
Zahlman
Hold on.

Are you talking about a vector, or about a vector which contains Derived*s?

These are different cases.

For the first, you will need a template. For the second, you just pass it in, and rely on polymorphism (virtual dispatch) on each pointed-to instance.

But if you're not using polymorphism, why are you storing pointers to instances instead of just storing instances?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.