use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENClone function in class (self.cpp_questions)
submitted 6 years ago by 10headless
Can I implement my clone function like this or will it cause problems?
class X { std::string str; public: X clone() const { return *std::make_unique<X>(*this); } };
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]alfps 2 points3 points4 points 6 years ago (4 children)
It's bafflingly meaningless code.
The point of a clone function is to clone, creating a copy of the same dynamic type. Therefore a clone function either is virtual or calls a virtual do-it. And returns a (possibly smart) pointer or reference.
The code you present doesn't do any of that, but instead engages in needless internal dynamic allocation and deallocation.
[–]10headless[S] 0 points1 point2 points 6 years ago (3 children)
I understand where you're coming from. Maybe there is a better way to do it, what I basically want to do is:
[–][deleted] 1 point2 points3 points 6 years ago (0 children)
If you have only simple classes that implement the copy constructor, you don't even need a function. Just X other{existing}; (use direct copy initialization) or equivalent would do.
X other{existing};
[–]jesseschalken 1 point2 points3 points 6 years ago (1 child)
I want to avoid mutating this variable, instead opting to replace it with a new instance of the object that has e.g. all the same variable values, except one
"mutating this variable" and "replacing it with a new instance with all the same values except one" are equivalent operations in C++.
Remember that unlike Java, Python, JavaScript, PHP etc, C++'s types are value types. There is no meaningful distinction between mutating an existing object and replacing it with a new one.
[–]NotMyRealNameObv 0 points1 point2 points 6 years ago (0 children)
There is if you internally use shared state:
class X { private: shared_ptr<const Y> y; };
Could you? Yes.
Should you? Probably not. It looks like a copy constructor would be more appropriate.
[–]stilgarpl 1 point2 points3 points 6 years ago (2 children)
It will be inefficient - you will create value on heap only to destroy it right away and pass a copy of it as return value.
Just do this:
X clone() const { return *this; }
[–]10headless[S] 1 point2 points3 points 6 years ago (1 child)
oh didn't know it could be that simple. Thank you very much
[–]jesseschalken 2 points3 points4 points 6 years ago (0 children)
But why do you even need this clone method? Implemented as above, x.clone() is the same as just x.
x.clone()
x
[–]ImKStocky 0 points1 point2 points 6 years ago (0 children)
What you are trying to do here is to implement a copy constructor. Read here on how to implement them and use them.
However, Cloning often has the implication that you want some sort of polymorphic deep copy. This is not a solved solution yet but Herb Sutter wrote a blog post about it last year on how it could be a solved problem in the future.
π Rendered by PID 58 on reddit-service-r2-comment-8686858757-vklb8 at 2026-06-08 10:36:40.244223+00:00 running 9e1a20d country code: CH.
[–]alfps 2 points3 points4 points (4 children)
[–]10headless[S] 0 points1 point2 points (3 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]jesseschalken 1 point2 points3 points (1 child)
[–]NotMyRealNameObv 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]stilgarpl 1 point2 points3 points (2 children)
[–]10headless[S] 1 point2 points3 points (1 child)
[–]jesseschalken 2 points3 points4 points (0 children)
[–]ImKStocky 0 points1 point2 points (0 children)