a vector to a class

V

Verbal Kint

Hallo everybody,

I am having a short question here which I can not answer myself. The
problem is, that I would like to give a vector to a class and modify
the vector with the help of the functions of the class. All the changes
inside the class should effect also the original vector.

In the following I am having a very limited code, that shows what I did
until now. But how to modify this code, so that all changes take place
outside the class? Is it possible to do this without giving the vector
as address operator in the constructor?

Thanks.
V.K.

class ClassA
{
public:
void SDat (vector <int> dat)
{
itsDat = dat;
}
private:
vector <int> itsDat;
};


int main()
{
int i;
ClassA CLASS;
std::vector <int> dat;

for (i=0;i<10;i++)
dat.push_back(i);

CLASS.SDat(dat);

return 0;
}
 
I

Ian Collins

Verbal said:
Hallo everybody,

I am having a short question here which I can not answer myself. The
problem is, that I would like to give a vector to a class and modify
the vector with the help of the functions of the class. All the changes
inside the class should effect also the original vector.

In the following I am having a very limited code, that shows what I did
until now. But how to modify this code, so that all changes take place
outside the class? Is it possible to do this without giving the vector
as address operator in the constructor?
Pass the vector by reference.
 
V

Verbal Kint

Excuse me if I may ask, but what is the difference between a REFERENCE
and the ADDRESS OPERATOR?

THANKS!
V.K.
 
I

Ian Collins

Verbal said:
Excuse me if I may ask, but what is the difference between a REFERENCE
and the ADDRESS OPERATOR?
What does your C++ book tell you?

Seriously, this isn't something to explain in a Usenet post.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Excuse me if I may ask, but what is the difference between a REFERENCE
and the ADDRESS OPERATOR?

Well, a reference is a type, the address-of operator is an operator,
just like a pointer is a type and the dereference operator and
multiplication operator are separate, if you can separate those you
should have little problem with reference. The multiplication operator
is easy to distinguish since it's a binary operator and thus require an
expression both in front and after. Both the dereference and address-of
operators are unary operators and goes before an expression (in this
case the expression is usually a variable). Lastly the pointer and
reference indicator (is there a good name for those?) goes after a
type-name, that's how you distinguish between them.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Hallo everybody,

I am having a short question here which I can not answer myself. The
problem is, that I would like to give a vector to a class and modify
the vector with the help of the functions of the class. All the changes
inside the class should effect also the original vector.

In the following I am having a very limited code, that shows what I did
until now. But how to modify this code, so that all changes take place
outside the class? Is it possible to do this without giving the vector
as address operator in the constructor?

As others have said, using references is the way to go, consider the
following code:

#include <iostream>
#include <vector>

class Test
{
std::vector<int>& data;

public:
Test(std::vector<int>& d) : data(d) { }
void print()
{
for (size_t i = 0; i < data.size(); ++i)
std::cout << data << "\n";
}
};

int main()
{
std::vector<int> lData;

// Create the Test-class with lData as parameter
Test test(lData);

// Change lData
for (size_t i = 0; i < 10; ++i)
lData.push_back(i);

// See that the vector in test also changed
test.print();

return 0;
}

Notice that Test only have a reference to a vector, it does not have an
actual vector as member, so the last comment is a bit of a lie.
 
V

Verbal Kint

Dear ERIK,

thanks for the help. i guess due to my none existing knowledge of
proper c++ expressions it came to the misunderstandings with address
operator and reference. because the code you wrote for me is what i
knew before already. thats why i mentioned in my first post:
"Is it possible to do this without giving the vector as address
operator in the constructor?"

i am just curious whether there is another to modify the vector in the
class.

once more thanks to you.
V.K.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

Dear ERIK,

thanks for the help. i guess due to my none existing knowledge of
proper c++ expressions it came to the misunderstandings with address
operator and reference. because the code you wrote for me is what i
knew before already. thats why i mentioned in my first post:
"Is it possible to do this without giving the vector as address
operator in the constructor?"

i am just curious whether there is another to modify the vector in the
class.

Well, there is either the reference or a pointer (in which case you
would have to use the address-of operator when calling the constructor)
but for most uses references are prefered.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top