Declaring a variable of user-defined type for late

2019-09-20 15:16发布

问题:

I want to create a global variable called process without assigning anything to it in a first moment. Later on I'll spawn a new process in the operating system, and assign it to that variable.

It can be done in C# like so:

class TestCS
    {
        // creating a variable
        private System.Diagnostics.Process process;

        private void SomeMethod()
        {
            // assigning a newly spawned process to it
            process = Process.Start("file.exe", "-argument");
            process.WaitForInputIdle();
        }       
    }


I wrote the code below to accomplish the same thing with C++. The process variable is of type child (from Boost::Process v0.31). #includes are omitted for simplicity.

Test.hpp

class Test
{
public: 
    void SomeFunction();
private:
    std::string testString; // declaring a test string

    static const std::string program_name;
    static const std::vector<std::string> program_args;
    boost::process::child process;  // attempting to declare a variable of type 'boost::process::child'
};

Test.cpp

void Test::SomeFunction()
{
    testString = "abc"; // I can successfully define the test variable on this line
    std::cout << testString;

    boost::process::context ctxt;

    // the same goes for the next two variables
    const std::string program_name = "startme.exe";
    const std::vector<std::string> program_args = {"/test"};

    // and I want to define the process variable here as well...
    process = boost::process::launch(program_name, program_args, ctxt);
}

Main.cpp

int main()
{
    Test test1;
    test1.SomeFunction();

    cin.get(); // pause
    return 0;
}

However, it returns the following error for Test.cpp:

error C2512: 'boost::process::child' : no appropriate default constructor available


How can it be done correctly?

回答1:

As the error states, 'boost::process::child' no appropriate default constructor available. This means that the child object must be constructed with a constructor that takes arguments.

Take the following example class

class Foo
{
    Foo(); // This is the _default_ constructor.
    Foo(int i); // This is another constructor.
};

Foo f; // The `Foo();` constructor will be used _by default_. 

If we change that class to the following:

class Foo
{
    Foo(int i); // This is the constructor, there is no default constructor declared.
};

Foo f; // This is invalid.
Foo f(1); // This is fine, as it satisfies arguments for `Foo(int i);

The reason your string is constructed is because it provides a default constructor (which is an empty string), while the process::child class does not.

Therefore, you need to initialize you process::child object when it's constructed. Since it's a part of TestClass (and not a pointer or a reference), it needs to be constructed when the TestClass object is constructed.

Test::Test()
    : process() // Initialize the member here, with whatever args.
{
    SomeFunction();
}

Or...

class Test
{
    // Make it a pointer.
    boost::process::child* pProcess;
};

Test::Test()
{
    pProcess = new boost::process::child(); // And allocate it whenever you want.
    SomeFunction();
}