dimanche 20 août 2017

Initialise static data members using lambda function

I am new to concept of using lambda functions in C++. My aim is to initialise a static data member array of objects using lambda function. Below is my code -

#include <iostream>
class B
{
    public:
    B() {std::cout << "B Called" <<std::endl;}
    B(int y){std::cout << "B int Called" <<std::endl;}
};
class A
{
    public:
    A(){std::cout << "Called" << std::endl;}
    static B bobj[256];
};

B bobj[256] = [] () {for (int i = 0 ; i < 256; i++) { bobj[i] = new B(2)}};

int main()
{
    A a;
}

But I am getting compilation error 'ambiguous overload for ‘operator=’ (operand types are ‘B’ and ‘B*’)' and others.

How can I code a lambda function to initialise an array of objects?

Aucun commentaire:

Enregistrer un commentaire