copy-constructor

How do I make this C++ object non-copyable?

会有一股神秘感。 提交于 2019-11-27 03:10:27
See title. I have: class Foo { private: Foo(); public: static Foo* create(); } What need I do from here to make Foo un-copyable? Thanks! Klaim class Foo { private: Foo(); Foo( const Foo& ); // non construction-copyable Foo& operator=( const Foo& ); // non copyable public: static Foo* create(); } If you're using boost, you can also inherit from noncopyable : http://www.boost.org/doc/libs/1_41_0/boost/noncopyable.hpp EDIT: C++11 version if you have a compiler supporting this feature: class Foo { private: Foo(); Foo( const Foo& ) = delete; // non construction-copyable Foo& operator=( const Foo& )

Creating a copy constructor for a linked list

梦想的初衷 提交于 2019-11-27 02:08:09
问题 This is homework I'm working on implementing a linked list class for my C++ class, and the copy constructor has be very confusing for me. The linked list is comprised of structs called Elems: struct Elem { int pri; data info; Elem * next; }; Elem * head; info is a separate, custom class that is stored in the Elem. the signature for the copy constructor is: linkedList::linkedList( const linkedList &v ) The issue I am having is mostly taking my logic and actually writing it as code. My general

Can we return objects having a deleted/private copy/move constructor by value from a function?

China☆狼群 提交于 2019-11-27 01:51:41
In C++03 it is impossible to return an object of a class having a private non-defined copy constructor by value: struct A { A(int x) { ... } private: A(A const&); }; A f() { return A(10); // error! return 10; // error too! } I was wondering, was this restriction lifted in C++11, making it possible to write functions having a class type return type for classes without constructors used for copy or move? I remember it could be useful to allow callers of a function use the newly returned object, but that they are not able to copy the value and store it somewhere. Here is how it can work A f() {

Why am I not provided with a default copy constructor from a volatile?

岁酱吖の 提交于 2019-11-27 01:33:19
问题 This code: class X { int member; }; volatile X a; X b = a; Fails with the error: prog.cpp:6:7: error: no matching function for call to ‘X::X(volatile X&)’ prog.cpp:6:7: note: candidates are: prog.cpp:1:7: note: X::X() prog.cpp:1:7: note: candidate expects 0 arguments, 1 provided prog.cpp:1:7: note: X::X(const X&) prog.cpp:1:7: note: no known conversion for argument 1 from ‘volatile X’ to ‘const X&’ Is there any way I can get the compiler to generate a volatile copy constructor for me? 回答1:

Is this good code? (copy constructor and assignment operator )

时光毁灭记忆、已成空白 提交于 2019-11-27 00:58:14
问题 For one reason or another, I'm forced to provide both a copy constructor and an operator= for my class. I thought I didn't need operator= if I defined a copy ctor, but QList wants one. Putting that aside, I hate code duplication, so is there anything wrong with doing it this way? Fixture::Fixture(const Fixture& f) { *this = f; } Fixture& Fixture::operator=(const Fixture& f) { m_shape = f.m_shape; m_friction = f.m_friction; m_restitution = f.m_restitution; m_density = f.m_density; m_isSensor =

Disable copy constructor

我只是一个虾纸丫 提交于 2019-11-27 00:03:48
I have a class : class SymbolIndexer { protected: SymbolIndexer ( ) { } public: static inline SymbolIndexer & GetUniqueInstance ( ) { static SymbolIndexer uniqueinstance_ ; return uniqueinstance_ ; } }; How should I modify it to disable code like: SymbolIndexer symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( ); and only allow code like : SymbolIndexer & ref_symbol_indexer_ = SymbolIndexer::GetUniqueInstance ( ); R. Martinho Fernandes You can make the copy constructor private and provide no implementation: private: SymbolIndexer(const SymbolIndexer&); Or in C++11, explicitly forbid it:

Why doesn't Java have a copy constructor?

笑着哭i 提交于 2019-11-27 00:02:13
Why doesn't Java support a copy constructor like in C++? Java does. They're just not called implicitly like they are in C++ and I suspect that's your real question. Firstly, a copy constructor is nothing more than: public class Blah { private int foo; public Blah() { } // public no-args constructor public Blah(Blah b) { foo = b.foo; } // copy constructor } Now C++ will implicitly call the copy constructor with a statement like this: Blah b2 = b1; Cloning/copying in that instance simply makes no sense in Java because all b1 and b2 are references and not value objects like they are in C++. In C+

Copy constructor is not inherited

这一生的挚爱 提交于 2019-11-26 22:51:33
I've got the following code: class C { public: C(int) {} C(const C&) {} C() {} }; class D : public C { public: using C::C; }; int main() { C c; D d_from_c(c); // does not compile, copy ctor is not inherited D d_from_int(1); // compiles, C(int) is inherited } Derived class should inherit all ctors of base except the default ctor (it is explained here ). But why copy ctor is not inherited as well? Arguments from the related question are not acceptable here. The code is compiled with g++ 4.8.1. Because the standard says so. [class.inhctor]/p3, emphasis mine: For each non-template constructor in

Copying derived entities using only base class pointers, (without exhaustive testing!) - C++

蹲街弑〆低调 提交于 2019-11-26 22:45:37
问题 Given a base class that is inherited by plethora of derived classes, and a program structure that requires you manage these via base class pointers to each entity. Is there a simple way to copy the entire derived object when only the base class pointer is known? Looking around it would seem possible (if incredibly tedious) to use the dynamic_cast call to check if a base pointer can be cast as the appropriate derived class, then copy it using the derived class's copy constructor. However this

Why is the copy constructor not called?

▼魔方 西西 提交于 2019-11-26 22:40:48
class MyClass { public: ~MyClass() {} MyClass():x(0), y(0){} //default constructor MyClass(int X, int Y):x(X), y(Y){} //user-defined constructor MyClass(const MyClass& tempObj):x(tempObj.x), y(tempObj.y){} //copy constructor private: int x; int y; }; int main() { MyClass MyObj(MyClass(1, 2)); //user-defined constructor was called. MyClass MyObj2(MyObj); //copy constructor was called. } In the first case, when MyClass(1, 2) calls the user-defined constructor and returns an object, I was expecting MyObj to call the copy constructor. Why it doesn't need to call the copy constructor for the second