我想意识到这一点,这段代码只是改变了构造函数的行为或其他东西。
我希望在相同情况下,该源代码的作用类似于设法更改构造功能。
MyClass具有构造函数,然后在构造中初始化私有值。 然后我想更改一些默认的初始化参数。
贝娄方法是一个合适的主意吗?
// ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma once
#include "pch.h"
#include <iostream>
#include <string>
class MyClass {
public:
MyClass(const std::string& InDescription)
: param1(false)
, param2(false)
, param3(true)
, param4(0)
, param5(1)
, param6(2)
, param7("A")
, param8("B")
, param9("C")
{
}
MyClass& PrintAllParam() {
std::cout << param1 << std::endl;
std::cout << param2 << std::endl;
std::cout << param3 << std::endl;
std::cout << param4 << std::endl;
std::cout << param5 << std::endl;
std::cout << param6 << std::endl;
std::cout << param7 << std::endl;
std::cout << param8 << std::endl;
std::cout << param9 << std::endl;
return *this;
}
MyClass& AsNonBlocking()
{
param1 = true;
return *this;
}
private:
bool param1;
bool param2;
bool param3;
int param4;
int param5;
int param6;
std::string param7;
std::string param8;
std::string param9;
};
int main()
{
std::cout << "Hello World!\n";
MyClass* ptrTest = nullptr;
std::string test = "ABC";
ptrTest = &MyClass(test)
.AsNonBlocking()
.PrintAllParam();
}