字段从方法返回后消失(C++ OOP) [复制]

Field disappears afrer return from method(C++ OOP) [duplicate]

提问人:Bonart 提问时间:9/17/2023 最后编辑:wohlstadBonart 更新时间:9/17/2023 访问量:34

问:

在这里,我试图在变量“a”中获取“Text”类的引用(这是链式方法所必需的),但是从值集字段返回引用后,字段就变成了空:

Text t = Text(base_text);
auto a = t.SetStrokeColor("yellow");

以下是带有模板等的 Text 类的定义:

//template PathProps class
class PathProps {
    public:
        Owner &SetFillColor(Color color) {
            fill_color_ = color;
            return AsOwner();
        }

        Owner &SetStrokeColor(Color color) {
            this->stroke_color_ = std::move(color);
            return AsOwner();
        }

        Owner &SetStrokeWidth(double width) {
            stroke_width_ = width;
            return AsOwner();
        }

        Owner &SetStrokeLineCap(StrokeLineCap strokeLineCap) {
            stroke_line_cap = strokeLineCap;
            return AsOwner();
        }

        Owner &SetStrokeLineJoin(StrokeLineJoin strokeLineJoin) {
            stroke_line_join = strokeLineJoin;
            return AsOwner();
        }

        void RenderAttrs(std::ostream &out) const {
            using namespace std::literals;

            if (fill_color_.has_value()) {
                out << " fill=\"" << *fill_color_ << "\"";
            }
            /*if (stroke_color_) {
                out << " stroke=\"" << *stroke_color_ << "\"";
            }*/
            if (stroke_width_.has_value()) {
                out << " stroke-width=\"" << *stroke_width_ << "\"";
            }
            if (stroke_line_cap.has_value()) {
                out << stroke_line_cap;
            }
            if (stroke_line_join.has_value()) {
                out << stroke_line_join;
            }
        }


    protected:
        ~PathProps() = default;

        Color stroke_color_;
        std::optional<Color> fill_color_;
        std::optional<double> stroke_width_;
        std::optional<StrokeLineCap> stroke_line_cap;
        std::optional<StrokeLineJoin> stroke_line_join;
    private:
        Owner &AsOwner() {
            return static_cast<Owner &>(*this);
        }
};

//Text class
class Text final : public Object, public PathProps<Text> {
    public:
        Text() = default;

        Text(const Text &);

        ~Text() override = default;

        Text &SetPosition(Point pos);

        Text &SetOffset(Point offset);

        Text &SetFontSize(uint32_t size);

        Text &SetFontFamily(std::string font_family);

        Text &SetFontWeight(std::string font_weight);

        Text &SetData(std::string data);

        [[nodiscard]]std::string replaceSpecialCharacters() const;

    private:

        Point TextCoordinates = {0, 0};
        Point TextShifts = {0, 0};
        uint32_t FontSize = 1;
        std::string FontWeight;
        std::string FontFamily;
        std::string text;

        void RenderObject(const RenderContext &context) const override;
};

我调试了它,并注意到方法中的值可以正常工作,但不能

C++ 模板 方法

评论

1赞 wohlstad 9/17/2023
您需要使用 ( 不包括引用性)。auto & a = ...auto

答:

1赞 Sam Varshavchik 9/17/2023 #1
auto a = t.SetStrokeColor("yellow");

这将获取从 返回的引用,并复制被引用对象,这将成为 .这不是参考。SetStrokeColoraa

你显然想成为一个参考,所以这应该是:a

auto &a = t.SetStrokeColor("yellow");

评论

0赞 Bonart 9/17/2023
谢谢,还有更多,如果我有一个按值获取的模板“添加”方法(类似的东西)doc.Add(Text{base_text}.SetStrokeColor("yellow"). SetFillColor("yellow") .SetStrokeLineJoin(StrokeLineJoin::ROUND) .SetStrokeLineCap(StrokeLineCap::ROUND).SetStrokeWidth(3))
0赞 Sam Varshavchik 9/17/2023
你不能。这不是一个“问题”。这就是C++的工作方式。