Problem Based Passing Vectors Elements As Arguments

A

Adam Hartshorne

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
 
I

Ivan Vecerina

....
: 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
 
P

Peter Jansson

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
 
V

Victor Bazarov

Adam said:
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<class ItX, class ItY> 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
 
H

Howard

Victor Bazarov said:
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
 
M

Malte Starostik

Howard said:
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
 
C

Chris Croughton

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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top