标签: copy-constructor

为什么C#不支持Copy Constructor的概念?

在我的一次采访中我被问到这个问题,但我无法确切地知道为什么这个概念不存在.

请告诉我.

c# copy-constructor

14
推荐指数
3
解决办法
8726
查看次数

复制QWidget

我需要能够复制Qwidget,因此我可以复制一个窗口,因为它会在运行时更改.这可能吗?

void Duplicate(QWidget * Show)
{
 //I tried...
 Qwidget Shw = *Show; //but operator= is private
 //and the copy constructor (I think), which is also private
 Qwidget Shw(*Show);
 //

 Shw.Show();
}
Run Code Online (Sandbox Code Playgroud)

c++ qt copy-constructor qwidget

14
推荐指数
1
解决办法
6971
查看次数

关于postblit和移动语义的问题

我刚才已经问了一个类似的问题,但我仍然不清楚一些细节.

  1. 在什么情况下postblit构造函数调用?

  2. 移动对象的语义是什么?它会被后照亮和/或破坏吗?

  3. 如果按值返回局部变量会发生什么?它会被隐含地移动吗?

  4. 如何将表达式转换为右值?例如,通用交换如何?

templates d return-value copy-constructor move-semantics

14
推荐指数
1
解决办法
896
查看次数

删除复制构造函数和复制赋值运算符.哪一项至关重要?

我有一个用例,我的对象不得以任何方式复制.我在下面写了一个夸张的复制构造函数和复制赋值运算符删除的完整列表.有这么多,我无法确定使用哪些,有时这让我变得偏执.我不必在我的代码中全部写出来,是吗?因此,为了防止任何类型的对象复制,我应该使用哪些对象?

        MyClass             (       MyClass &)  = delete;
        MyClass             (const  MyClass &)  = delete;
        MyClass             (       MyClass &&) = delete;
        MyClass             (const  MyClass &&) = delete;
        MyClass    operator=(       MyClass &)  = delete;
        MyClass    operator=(const  MyClass &)  = delete;
const   MyClass    operator=(       MyClass &)  = delete;
const   MyClass    operator=(const  MyClass &)  = delete;
        MyClass &  operator=(       MyClass &)  = delete;
        MyClass &  operator=(const  MyClass &)  = delete;
const   MyClass &  operator=(       MyClass &)  = delete;
const   MyClass &  operator=(const  MyClass &)  = …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor copy-assignment deleted-functions implicit-methods

14
推荐指数
2
解决办法
6926
查看次数

为什么复制构造函数在此代码段中调用了两次?

我正在玩一些东西来理解复制构造函数的工作原理.但我无法理解为什么复制构造函数被调用两次才能创建x2.我会假设在createX()复制返回值时会调用一次x2.
我还看了一些关于SO的相关问题,但据我所知,我找不到与我在这里问的相同的简单场景.

顺便说一句,我正在编译,-fno-elide-constructors以便在没有优化的情况下查看正在发生的事情.

#include <iostream>

struct X {
    int i{2};

    X() {
        std::cout << "default constructor called" << std::endl;
    }

    X(const X& other) {
        std::cout << "copy constructor called" << std::endl;
    }
};

X createX() {
    X x;
    std::cout << "created x on the stack" << std::endl;
    return x;
}

int main() {
    X x1;
    std::cout << "created x1" << std::endl;
    std::cout << "x1: " << x1.i << std::endl << std::endl;    

    X …
Run Code Online (Sandbox Code Playgroud)

c++ copy-constructor c++14

14
推荐指数
2
解决办法
1219
查看次数

从复制构造函数调用默认赋值运算符是不好的形式?

考虑一类需要复制的副本.副本中的绝大多数数据元素必须严格反映原始数据元素,但是有少数元素的状态不被保留且需要重新初始化.

从复制构造函数调用默认赋值运算符是不好的形式?

默认赋值运算符对于Plain Old Data(int,double,char,short)以及每个赋值运算符的用户定义类都表现良好.指针需要单独处理.

一个缺点是该方法使得赋值运算符瘫痪,因为不执行额外的重新初始化.也无法禁用赋值运算符的使用,从而通过使用不完整的默认赋值运算符打开用户创建损坏类的选项A obj1,obj2; obj2=obj1; /* Could result is an incorrectly initialized obj2 */.

a(orig.a),b(orig.b)...除了a(0),b(0) ...必须写之外,放宽要求是很好的.需要写入所有初始化两次会产生两个错误位置,如果double x,y,z要将新变量(例如)添加到类中,初始化代码需要在至少2个位置而不是1个位置正确添加.

有没有更好的办法?

在C++ 0x中有更好的方法吗?

class A {
  public:
    A(): a(0),b(0),c(0),d(0)
    A(const A & orig){
      *this = orig;       /* <----- is this "bad"? */
      c = int();
    }
  public:
    int a,b,c,d;
};

A X;
X.a = 123;
X.b = 456;
X.c = 789;
X.d = 987;

A Y(X);

printf("X: %d %d %d %d\n",X.a,X.b,X.c,X.d); …
Run Code Online (Sandbox Code Playgroud)

c++ constructor copy-constructor assignment-operator c++11

13
推荐指数
2
解决办法
4698
查看次数

我认为数组是不可复制的

我的印象是阵列是不可复制的(或可分配的).

int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,0};

x = y; // Fails to compile
Run Code Online (Sandbox Code Playgroud)

但是当我在一个类中放入一个数组时,复制构造函数和赋值运算符工作(我会按预期说,但它不是我所期望的).

#include <iostream>

struct X
{
    virtual ~X(){} // Just in case it was something to do with POD 
                   // make sure its not a POD
    int x[5];
};

int main()
{
    X   a;

    a.x[0]  = 0;
    a.x[1]  = 1;
    a.x[2]  = 2;
    a.x[3]  = 3;
    a.x[4]  = 4;

    // Make a copy of a and test it
    X   b(a);          
    std::cout << a.x[0] << …
Run Code Online (Sandbox Code Playgroud)

c++ arrays copy-constructor assignment-operator

13
推荐指数
1
解决办法
690
查看次数

std :: string copy构造函数不在GCC 4.1.2中?

我想知道我是否误解了某些内容:复制构造函数是否std::string 复制其内容?

string str1 = "Hello World";
string str2(str1);

if(str1.c_str() == str2.c_str()) // Same pointers!
  printf ("You will get into the IPC hell very soon!!");
Run Code Online (Sandbox Code Playgroud)

这将打印出"你很快就会进入IPC地狱!" 它让我烦恼

这是正常的行为std::string吗?我在某处读到它通常会做一个深层复制.

但是,这可以按预期工作:

string str3(str1.c_str());

if(str1.c_str() == str3.c_str()) // Different pointers!
  printf ("You will get into the IPC hell very soon!!");
else
  printf ("You are safe! This time!");
Run Code Online (Sandbox Code Playgroud)

它将内容复制到新字符串中.

c++ gcc deep-copy stdstring copy-constructor

13
推荐指数
2
解决办法
3276
查看次数

VC++ 15为lambda捕获调用错误的拷贝构造函数?

考虑以下程序:

#include <iostream>
struct X {
  X () = default;
  X (X &) { std::cout << "non-const called" << std::endl; }
  X (X const &) { std::cout << "const called" << std::endl; }
  int i () const { return 7; }
};

auto f () {
  X x;
  auto lambda = [=]() { return x.i(); };
  return lambda;
}

int main()
{
  auto lambda = f();
  std::cout << lambda () << std::endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

使用VC++ 15,我得到了输出

const called
const …
Run Code Online (Sandbox Code Playgroud)

c++ lambda clang copy-constructor visual-c++

13
推荐指数
1
解决办法
226
查看次数

是否可以将容器的复制构造函数定义为已删除的非可复制值类型?

如果我们有一个具有不可复制值类型的容器,这样的容器类仍然定义了复制构造函数,只是它可能不会被调用.

using T = std::vector<std::unique_ptr<int>>;
std::cout << std::is_copy_constructible_v<T>; // prints out "1" (libstdc++)
Run Code Online (Sandbox Code Playgroud)

这可能会导致"隐藏"问题,例如此处讨论的问题:Visual Studio 2017是否需要显式移动构造函数声明?.

我的问题是标准库实现是否可以将此类复制构造函数定义为有条件删除,即在不可复制的值类型的情况下删除.这对我来说是完全合理的(至少在有C++概念之前).这种实施是否符合标准?

c++ containers copy-constructor noncopyable language-lawyer

13
推荐指数
1
解决办法
597
查看次数