-
Jun 24th, 2014, 05:49 AM
#1
Thread Starter
Member
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?
-
Jun 30th, 2014, 02:11 AM
#2
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:
#include <iostream> #include <vector> class parent_obj { public: parent_obj(int x) : _x(x) {} const int &get_x() const { return _x; } private: int _x; }; template <class T> class myobj { public: myobj(int id, T *parent) : _id(id), _parent(parent) {} const int &get_id() const { return _id; } const T *get_parent() const { return _parent; } private: int _id; T *_parent; }; int main() { parent_obj p_obj(20); std::vector<myobj<parent_obj>> v; for (int i = 0; i < 10; ++i) v.push_back(myobj<parent_obj>(i + 1, &p_obj)); for (const auto &it : v) { const parent_obj *p = it.get_parent(); std::cout << "myboj id:" << it.get_id() << " - parent {address:" << p << "} id = " << p->get_x() << std::endl; } }
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Jun 30th, 2014, 02:48 AM
#3
Thread Starter
Member
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.
-
Jun 30th, 2014, 02:58 AM
#4
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.
<<<------------
.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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|