446,388 Members | 1,761 Online Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

# Problem Based Passing Vectors Elements As Arguments

 P: n/a Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) } } loop using the vector iterators. Ok well f(x) calls another function, and i want to pass pointers to the elements in the two vectors that the current iterators point at to this function. Well i know that the iterator class is basically just a pointer to the position in the vector anyway. However I want f(x) to remain general and take a pointers to a type X and a type Y as arguments, rather than two iterators. Ok so my question is what do i pass? Is it something like f(&(*IteratorX), &(*IteratorY))? Or is there a better way. If my f function took references as arguments, how if at all can i set a variable contained in X which is a pointer to type Y to the argument of the function f which is a type Y? i.e. an element of type X in the vector of type X's now contains a pointer to an element in the vector of type Y's contained in the main method! Adam Jul 23 '05 #1
6 Replies

 P: n/a "Adam Hartshorne" wrote in message news:d1**********@wisteria.csv.warwick.ac.uk... .... : Ok well f(x) calls another function, and i want to pass pointers to the : elements in the two vectors that the current iterators point at to this : function. : : Well i know that the iterator class is basically just a pointer to the : position in the vector anyway. However I want f(x) to remain general and : take a pointers to a type X and a type Y as arguments, rather than two : iterators. : : Ok so my question is what do i pass? Is it something like : f(&(*IteratorX), &(*IteratorY))? Or is there a better way. Yes. You can as well drop two pairs of parentheses: f( &*IteratorX, &*IteratorY ); But if either (or both) of the function's arguments is never supposed to be NULL, then passing references (and if possible a const&) instead of a pointer is probably a good idea. The calling syntax will then become: f( *IteratorX, *IteratorY ); : If my f function took references as arguments, how if at all can i set a : variable contained in X which is a pointer to type Y to the argument of : the function f which is a type Y? i.e. an element of type X in the : vector of type X's now contains a pointer to an element in the vector of : type Y's contained in the main method! void f( TypeX& x, TypeY& y ) { x.field = & y; // here '&' is the address-of operator } Ivan -- http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form Jul 23 '05 #2

 P: n/a > I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) } } loop using the vector iterators. Ok well f(x) calls another function, and i want to pass pointers to the elements in the two vectors that the current iterators point at to this function. Well i know that the iterator class is basically just a pointer to the position in the vector anyway. However I want f(x) to remain general and take a pointers to a type X and a type Y as arguments, rather than two iterators. Ok so my question is what do i pass? Is it something like f(&(*IteratorX), &(*IteratorY))? Or is there a better way. Why not the following? f(X&,Y&) {...} Which you call by: f(*IteratorX,*IteratorY); If my f function took references as arguments, how if at all can i set a variable contained in X which is a pointer to type Y to the argument of the function f which is a type Y? i.e. an element of type X in the vector of type X's now contains a pointer to an element in the vector of type Y's contained in the main method! Could you please explain your problem above more clearly? Regards, Peter Jul 23 '05 #3

 P: n/a Adam Hartshorne wrote: I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) } } loop using the vector iterators. Ok well f(x) calls another function, and i want to pass pointers to the elements in the two vectors that the current iterators point at to this function. Well i know that the iterator class is basically just a pointer No, it isn't. to the position in the vector anyway. However I want f(x) to remain general and take a pointers to a type X and a type Y as arguments, rather than two iterators. Actually, I think it would be better if you make it a template and pass the iterators there. Pointers *are* iterators: template void f(ItX px, ItY py) { ... // use px-> or *px here, should work for iterators (and pointers) } Ok so my question is what do i pass? Is it something like f(&(*IteratorX), &(*IteratorY))? Or is there a better way. In what way should it be better? That's the only legal way I know. If my f function took references as arguments, how if at all can i set a variable contained in X which is a pointer to type Y to the argument of the function f which is a type Y? i.e. an element of type X in the vector of type X's now contains a pointer to an element in the vector of type Y's contained in the main method! You should use C++ to describe the relationships between types here. So, class X { Y *py; public: void set_py(Y *py) { this->py = py; } }; class Y {}; void f(X& rx, Y& ry) { rx.set_py(&ry); } V Jul 23 '05 #4

 P: n/a "Victor Bazarov" wrote in message news:CFi0e.55702> class X { Y *py; public: void set_py(Y *py) { this->py = py; } }; Is that ok? I mean, you've got the same name for the parameter as the member variable. Is the right side of that assignment guaranteed to refer to the parameter and not the member? (Even if it is, I'd avoid the practice and give it a different name, just to avoid confusion.) -Howard Jul 23 '05 #5

 P: n/a Howard schrieb: "Victor Bazarov" wrote in message news:CFi0e.55702> class X { Y *py; public: void set_py(Y *py) { this->py = py; } }; Is that ok? I mean, you've got the same name for the parameter as the member variable. Is the right side of that assignment guaranteed to refer to the parameter and not the member? Yes, local variables and parameters shadow members. this-> can be used to refer to the shadowed member. (Even if it is, I'd avoid the practice and give it a different name, just to avoid confusion.) Indeed. For trivial cases like above I think it's acceptable though. I always prefix members with m_ in order not to run out of names for locals :-) Cheers, Malte Jul 23 '05 #6

 P: n/a On Wed, 23 Mar 2005 20:01:54 +0100, Malte Starostik wrote: Howard schrieb: "Victor Bazarov" wrote in message news:CFi0e.55702> class X { Y *py; public: void set_py(Y *py) { this->py = py; } }; Is that ok? I mean, you've got the same name for the parameter as the member variable. Is the right side of that assignment guaranteed to refer to the parameter and not the member? Yes, local variables and parameters shadow members. this-> can be used to refer to the shadowed member. It's particularly useful for initialisers, where the only time the member is used is to set it. (Even if it is, I'd avoid the practice and give it a different name, just to avoid confusion.) Indeed. For trivial cases like above I think it's acceptable though. I always prefix members with m_ in order not to run out of names for locals :-) I do the opposite, I prefix parameters with a so I write ClassName(int aVal) : val(aVal) It's all just a coding style convention, though, and I've worked in places with a load of different convention on naming variables (one where all parameters were aThing, member variables mThing, local variables started lowercase (not followed by a lowercase letter), member functions starting uppercase, file local functions starting lowercase (so like local variables but followed by ( when calling them), etc. Chris C Jul 23 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion. 