Results 1 to 4 of 4

Thread: Using constuctor in class or vector

  1. #1

    Thread Starter
    Member
    Join Date
    May 2014
    Posts
    44

    Using constuctor in class or vector

    I illustrate my problem on this example. Let's have a class A and B.

    Code:
    class A;
    class B{
    int m;
    A * pParentHold;
    public:
    B(int n, A * pParent){m=n;pParentHold=pParent;}
    };
    
    class A{
    public:
    B test(1, this);
    std::vector<B> MyVector;
    };
    I know how to pass arguments to constructor of class B, but I need to know how can I pass the arguments to the constructor of B when it is created by vector.

    When I would do something like
    std::vector<B> MyVector(arguments...); so it will pass arguments to the vector constructor (to the template of the vector)... However what if I want every object of B which is stored by vector MyVector to be holding reference to the parent object A?

  2. #2
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Using constuctor in class or vector

    What is the goal here exactly? This is improper code to begin with... :S

    A vector of type B doesn't mean that they are initialized right away, you need to do this manually to populate the container.

    Why are you trying to pass arguments to the vector constructor? The vector object and the data the vector holds as it is a container, are completely different. The vector itself needs to be constructed as well as each object that the vector contains (of type class B).

    Here's an example:
    cpp Code:
    1. #include <iostream>
    2. #include <vector>
    3.  
    4. class parent_obj
    5. {
    6. public:
    7.   parent_obj(int x) : _x(x) {}
    8.   const int &get_x() const { return _x; }
    9. private:
    10.   int _x;
    11. };
    12.  
    13. template <class T>
    14. class myobj
    15. {
    16. public:
    17.   myobj(int id, T *parent)
    18.     : _id(id), _parent(parent) {}
    19.   const int &get_id() const { return _id; }
    20.   const T *get_parent() const { return _parent; }
    21. private:
    22.   int _id;
    23.   T *_parent;
    24. };
    25.  
    26. int main()
    27. {
    28.   parent_obj p_obj(20);
    29.   std::vector<myobj<parent_obj>> v;
    30.   for (int i = 0; i < 10; ++i)
    31.     v.push_back(myobj<parent_obj>(i + 1, &p_obj));
    32.  
    33.   for (const auto &it : v)
    34.   {
    35.     const parent_obj *p = it.get_parent();
    36.     std::cout << "myboj id:" << it.get_id()
    37.               << " - parent {address:" << p
    38.               << "} id = " << p->get_x()
    39.               << std::endl;
    40.   }
    41. }

    Output:
    Code:
    myboj id:1 - parent {address:0x28ff04} id = 20
    myboj id:2 - parent {address:0x28ff04} id = 20
    myboj id:3 - parent {address:0x28ff04} id = 20
    myboj id:4 - parent {address:0x28ff04} id = 20
    myboj id:5 - parent {address:0x28ff04} id = 20
    myboj id:6 - parent {address:0x28ff04} id = 20
    myboj id:7 - parent {address:0x28ff04} id = 20
    myboj id:8 - parent {address:0x28ff04} id = 20
    myboj id:9 - parent {address:0x28ff04} id = 20
    myboj id:10 - parent {address:0x28ff04} id = 20
    Last edited by AceInfinity; Jun 30th, 2014 at 02:31 AM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  3. #3

    Thread Starter
    Member
    Join Date
    May 2014
    Posts
    44

    Re: Using constuctor in class or vector

    Sorry, but could you please use syntax for C++03? Line 33 and 35 are printing errors to my C++03.

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Using constuctor in class or vector

    http://en.cppreference.com/w/cpp/language/range-for

    Just change it back to an iterator for loop then.

    Define the iterator, move it to the next position while it's != vector end(). That code is just for displaying the data, it's not necessary here anyways.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width