1

If we have a class with a constructor to be initialized in another class's constructor we do the following,

Class A{
 A(int arg):a(arg);
 int a;
};
Class B{
 B(int arg):A_obj(arg);
 A A_obj;
}

I have encountered a situation like the following,

Class B{
 B(int arg1,int arg2);
 A *A_obj//Array of objects of A of size arg2;
}

Apart from initializing this in a serparate function, is there any way this can be done using the constructor initializer?? Also are there any other elegant ways in which this can be handeled.

Thanks!

Edit: I was hoping for some kind of loop functionality within B's constructor initializer.

Edit: Cory's Answer sorts this out. I was wondering if we could further pass an iterating value as an input argument to every instance of A in the A_obj array.

class A{
 A(int arg,std::string n);
 int a;
 std::string name;
}

Something on the lines of A_obj(arg2,A(arg1,"Name"+str(iterator))

Solution (Thanks to Cory)

#include <iostream>
#include<string>
#include <vector>

class A{
public:
    A(int arg,std::string n):a(arg),name(n){};
    int a;
    std::string name;
    void sayhi(){std::cout<<"Hi from "<<name<<std::endl;}
};

class B{
public:
    B(int arg1, int arg2){
        A_obj.reserve(arg2);
        for (std::size_t i = 0; i < arg2; ++i){
            A_obj.emplace_back(arg1, "Name" + std::to_string(i));  // create new A object and add to vector
        }
    }std::vector<A> A_obj;
};

int main(){
    B B_obj(1,10);
    for(int i=0;i<10;i++){
        B_obj.A_obj[i].sayhi();
    }
    return 0;
}
2
  • Are you asking how to set the array size at run-time? That's not allowed in c++. Commented Jun 12, 2020 at 18:31
  • Sorry, I meant it to be allocated through 'new' Commented Jun 12, 2020 at 18:32

1 Answer 1

2

I would suggest changing your raw array to a std::vector

class B{
 B(int arg1,int arg2);
 std::vector<A> A_obj;
};

Then you can use the member initialization list

B::B(int arg1, int arg2) : A_obj(arg2) {}

This will size your vector with arg2 number of default-initialized A objects.

If you want each A to be initialized with arg1 then you use the other vector constructor

B::B(int arg1, int arg2) : A_obj(arg2, A(arg1)) {}

To pass different values to each object, you'd need to do that in the constructor body

B::B(int arg1, int arg2)
{
    A_obj.reserve(arg2);
    for (std::size_t i = 0; i < arg2; ++i)
    {
        A_obj.emplace_back(arg1, "Name" + std::to_string(i));  // create new A object and add to vector
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.. This way I cannot have the constructor of A to accept the arg1
@Some_guy If you want to pass arg1, then see the edit on the bottom of my answer, you can create arg2 number of objects, where each one is a copy of an A constructed with argument arg1
This is awesome... I was wondering if it would be possible to further pass an iterating number along with arg1 (sorry this is not represented in the question I have posted). Like A_obj[0]->A(arg1,0) , A_obj[1]->A(arg1,1)
@Some_guy You cannot do that in the member initialization list, but you can do that in the constructor body. See bottom-most edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.