private vector intializer

closed account (SECMoG1T)
Hi there, is it possible to intialize a private container members
Such as a vector in a class.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  class intial
   {
     Public:
         /// some stuff here
     Private:
         Vector< otherclass > member(5);// is this possible
   };

Or 
   class temp
    {
      Public:
          /// stuff here
      Private:
          Static Constexpr size_t foo=4;
          Vector <otherclass> myvec (foo);
          /// is this possible. 
    };
Last edited on
Yes, it is possible. But you need to use C++11 uniform initialisation feature:

1
2
3
4
class foo 
{
    std::vector<bar> member {5}; //By default calls vector<T>(int) constructor
};

Or use constructors:
1
2
3
4
5
6
class foo
{
    std::vector<bar> member;
  public:
    foo() : member(5) {}
}
closed account (SECMoG1T)
@Minippa thank you for you suggestion
Well thats great but am havin hell here doing the same, on my
First example am getting an error
 
Expected , or ... before numeric  constant. 

On the second example im getting an error too
 
Foo is not a type .


Well am puzzled, I need it to work, what is best to do for such a scenario
Hawt wrong with my xamples above? I'll appreciate greatly .
Foo
Post the code here. I hope you do not mixing capital and small letters, do you?
closed account (SECMoG1T)
well @miniippa your way works, I have tested by providing a contructor
 
member(5)


And am wondering too, what if I wanted to provide arguments to each of
The five objects constructor at the same time is it possible? How can that be done
If possible?
1
2
3
4
5
6
7
8
9
10
class foo
{
    int              i;
    double           d;
    char             c;
    std::string      s;
    std::vector<int> v;
  public:
    foo() : i(0), d(3.14), c('a'), s("Hello"), v({1, 2, 3, 4, 5}) {}
}
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
Last edited on
closed account (SECMoG1T)
Haha im now on track @minnippa you seem to be having all I need, Thank you so much
All your ways are working just fine , it is great n am putting it to practice, ill get back if a thing fails
Good job.
closed account (SECMoG1T)
Thanks for the help, I will be glad too if you help me understand the mystery on my second example
 Foo is not a type .

Check my code below though I won't use it no more , just to clear the dlbig question "why?".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class other
 {
    public:
          other ():data (0) {}
          other (int x):data (x){ }
    private:
           Int data;
 };

 class myclass
 {
   public:
       myclass ()=default; 
    private:
        static constexpr size_t foo=5;
        std::vector <other> myvec (foo); // what's wrong here ??
 };


Help me understand this bug please.
1) line 7 Int data; should be: int data;
2) std::vector <other> myvec (foo); is not doing what do you want here!
It declares a member function which takes and object of (nonexistent) type foo. You need to use uniform initialization to give default value for that member: std::vector <other> myvec {foo}; .
Note the curly braces {} instead of usual ()
closed account (SECMoG1T)
Wow wow ! Exactly, how the hell didn't I see that? Yeah I errorneously made a function instead of
Vector variable , thank you so much @minnippa uniform intialization seems to be a thing am missing , av tested std::vector <other> myvec {foo};  it n it's working now
This av been a big time question now it is fully solved, Thenk you for your time, that was great , can't believe it.
Topic archived. No new replies allowed.