A C ++ class element is an object that requires a constructor ... that requires a function to instantiate

The question I'm asking comes up many times, but not enough for mine. In my case, I have a class (allows calling C0) that has a member that is a class (call that C1) that requires a constructor. However, in my case, I want to do some processing before passing the variable to C1's constructor. Is there a way to do this? In essence, I would like to achieve something like this: (which of course doesn't work)

class C0{
public:
  C0(){
    ComplexDataType data;
    //Do some process with data
    myC1(data);
   }
private:
  C1 myC1;
};

class C1{
public:
  C1(ComplexDataType data);
}

      

I've seen the initialization list from here , but my only thought on how to do this would result in a VERY ugly initialization list, something like this: (works)

class C0{
public:
  C0() : variable1(doSomething1), variable2(doAnotherThing), variable3(keepItWorking), data(finallyDoSomethingWithTheOtherVariables), c1(data)
{
  //Something
};

class C1{
//Stuff
};

      

Is there an elegant way to achieve what you want?

Update Oh yes, I forgot to mention: I can't change anything in class C1

+3


source to share


4 answers


If I understood correctly, it looks like you want to do some processing and then initialize your class member using an initialization list.

You can use a static member to achieve this, for example:



class C0{
public:
  C0():  myC1(process()){

    //Do some process with data

   }
private:
  C1 myC1;

    static ComplexDataType process()
    {
        ComplexDataType data;
        // ... do stuff
        return data ;
    }
};

      

+2




I see a couple of ways to solve this problem

You can hold the pointer to myC1 at C0. then inside C0 c'tor you can process your data and then set myC1 = new myC1 (data);

class C0 {
public:
  C0() {
    ComplexDataType data;
    //Do some process with data
    myC1 = new C1(data);
  }
private:
  C1 *myC1;
};

      




Or, if it makes sense, you can add a new class that will do preliminary work in your c'tor and declare it (and thus create it) before myC1 in C0, and then pass its result (using the get method) to myC1 when creation.

class DataConfiguration {
public:
  DataConfiguration() {
    //Do some process with data 
  }
  const ComplexDataType &getData() {
    return data;
  }
private:
  ComplexDataType data;
};

class C0 {
public:
  C0() :
      dataConf(),
      myC1(dataConf.getData()) {
  }
private:
  DataConfiguration dataConf;
  C1 myC1;
};

      

+2


source


You can implement the copy assignment operator for C1

( which you probably should anyway ) and in the constructor C0

just like eg myC1 = C1(data);

.

+1


source


My guess is that you cannot change the class C1

, so you should seriously consider using pointers. You can even use shared_ptr

it to forget about freeing memory when you no longer need the object.

class C0{
public:
  C0(){
    ComplexDataType data;
    //Do some process with data
    myC1 = std::make_shared<C1>(data);
   }
private:
  std::shared_ptr<C1> myC1;
};

      

+1


source







All Articles