a vector to a class

Discussion in 'C++' started by Verbal Kint, Jan 18, 2007.

  1. Verbal Kint

    Verbal Kint Guest

    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;
    }
    Verbal Kint, Jan 18, 2007
    #1
    1. Advertising

  2. Verbal Kint

    Ian Collins Guest

    Verbal Kint wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Jan 18, 2007
    #2
    1. Advertising

  3. Verbal Kint

    sHacHaosHu Guest

    could not reference help?

    "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;
    > }
    sHacHaosHu, Jan 18, 2007
    #3
  4. Verbal Kint

    Verbal Kint Guest

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

    THANKS!
    V.K.
    Verbal Kint, Jan 18, 2007
    #4
  5. Verbal Kint

    Ian Collins Guest

    Verbal Kint wrote:
    > 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.

    --
    Ian Collins.
    Ian Collins, Jan 18, 2007
    #5
  6. On Jan 18, 2:55 am, "Verbal Kint" <> wrote:
    > 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.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 18, 2007
    #6
  7. On Jan 18, 2:31 am, "Verbal Kint" <> wrote:
    > 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.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 18, 2007
    #7
  8. Verbal Kint

    Verbal Kint Guest

    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.
    Verbal Kint, Jan 19, 2007
    #8
  9. On Jan 19, 7:07 am, "Verbal Kint" <> wrote:
    > 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.

    --
    Erik Wikström
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Jan 19, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    8
    Views:
    848
    Dhruv
    Jul 2, 2003
  2. pmatos
    Replies:
    6
    Views:
    23,787
  3. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    357
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page