我不明白C++中赋值构造函数和复制构造函数的区别。它是这样的:
class A {
public:
A() {
cout << "A::A()" << endl;
}
};
// The copy constructor
A a = b;
// The assignment constructor
A c;
c = a;
// Is it right?
我想知道如何分配赋值构造函数和复制构造函数的内存?
如果有,原因何在?为什么它不使用值类型的复制构造函数?
我得到以下错误:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio
n `ClassWithoutAss& ClassWithoutAss::operator=(const ClassWithoutAss&)':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: instantiate
d from `void std
我有一些关于C++中构造函数的问题。对于每个问题(从(1)到(4)),我想知道行为是否完全符合标准。
A)第一个是关于成员的初始化:
class Class
{
public:
Class()
: _x(0)
, _y(_x)
, _z(_y) // <- (1) Can I initialize a member with other members ?
{
;
}
protected:
int _x;
int _y
在使用g++ -std=c++0x进行编译时,我遇到了一些将T类型的元素推回到向量的编译问题。
这是一个最小的示例:
#include <vector>
using namespace std;
class A {
public:
A() { }
A& operator=(A &orig) {
return *this;
}
};
int main(int argc, char **argv) {
A a;
vector<A> b;
A c = a; // This is fine
以下声明通过编译检查:
int arr[3];
vector<int[3]> vec; // ok !!
当尝试实际使用vec时,
vec.push_back(arr);
它会导致许多错误,如:
/usr/include/c++/4.6/ext/new_allocator.h:108:9: error: ISO C++ forbids initialization in array new [-fpermissive]
/usr/include/c++/4.6/bits/vector.tcc:314:4: error: invalid array assignment
/usr
这段代码
#include <memory>
#include <vector>
class Foo
{
public:
~Foo()
{
}
std::unique_ptr<int> bar;
};
int main()
{
std::vector<Foo> foos;
foos.emplace_back();
}
在g++中产生以下错误消息:
In file included from /usr/include/c++/4.8/memory:64:0,
f
我是C++的新手。我正在用SFML和Box2D开发一个中断克隆,在编译时我得到了这个错误。错误的详细信息:
c:\program files (x86)\visual studio express 2013\vc\include\xutility(2420): error C2280: 'Tile &Tile::operator =(const Tile &)' : attempting to reference a deleted function
c:\users\harry\documents\visual studio 2013\projects\cpp
这是一个古老的考题,它要求我们编写赋值运算符,并在有意义的时候复制构造函数和析构函数。
给定以下代码:
class U { /* code not specified here */ };
class A { /* code not specified here */ private: U& u_; };
我了解到答案是:A持有对U实例的C++引用,它可以被复制,但不能被重置。因此,您必须:
·编写一个复制构造函数,将其U初始化为A源实例所引用的相同实例。
·将其赋值运算符设置为私有,并且不实现
我知道引用是不能重置的。然而,这是否意味着只要类包含引用成员数据,我就永远不能使用赋值运算
我是C++的新手,现在我正在尝试实现一个模型。那么,为类的每个成员重载赋值操作符(作为setter)是否有意义?如果非成员函数试图为私有/受保护的数据成员设置新值,这将非常重要。例如:
class Human {
private:
double aWeight[100]; /* let's say I want to keep...
...a set of 100 weight measures of this human.*/
// member functions and constructors here...
};
现在,假设我有一个非会员
我想知道赋值操作符的默认实现是否检查自赋值,因此这两种实现中哪一种可以被认为是最接近默认的:
class A{
int x;
public :
...
// first one
A& operator=(const A& a){
if(this != &a) x = a.x;
return *this;
}
// second one
A& operator=(const A& a){
x = a.x;
return *this;
这是编译器的闪屏(用于版本等):
C:\Program Files\Microsoft Visual Studio 10.0\VC>cl.exe
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.
usage: cl [ option... ] filename... [ /link linkoption... ]
我有一个基类(它是一个模板),想象它是:
tem
我不断地收到以下错误消息,告诉我正在使用一个deleted函数,我认为它是std::variant默认构造函数。 In file included from main.cpp:2:
Document.hpp: In instantiation of ‘Document<StateVariant, EventVariant, Transitions>::Document(StateVariant&&) [with StateVariant = std::variant<DraftState, PublishState>; EventVariant = s
代码中使用的这两种代码之间的区别是什么,就像我使用过的一样。(第44行广告第45行都很好)
节选:
Date temp = *this; //ASSIGNMENT OPERATOR CALL(PROVIDED BY COMPILER)
//Date temp(*this); //COPY CONSTRUCTOR CALL(PROVIDED BY COMPILER)
My via :在分配过程中,是否object1 = object2;object2的内容被删除并放置在object1中,而如果通过复制构造函数发生相同的事情,object2的内容仍然保留(我的意思是“复制”这个词的意思)。
注意
在c++中,我在bruce中看到,不能自动继承的函数是:
建设者
破坏者
运算符=(因为它执行类似于构造函数的操作)
但这段代码说明了另外一种情况
#include<iostream>
using namespace std;`
class A {
public:
A & operator= (A &a) {
cout<<" base class assignment operator called ";
return *this
C++中的一种常见模式是将复制构造函数设为私有:
class A
{
public:
// ...
private:
A(const A&);
};
但是下面的代码将会编译(在C++11/14中):
A f();
auto a = f();
该标准包含有关自动生成移动构造函数的信息。我既不能使用标准,也不能使用编译器来生成移动构造器。我的问题是:我必须写吗?
class A
{
public:
// ...
private:
A(const A&);
A(const
我正在创建一个跟踪学生的类。在这个类中,我使用一个重载的=来复制这些学生对象。为了跟踪它们的类,我使用了一个动态数组。该数组复制得很好;但是,当清除学生对象的变量时,以前从该对象复制的任何对象的数组也会被擦除。代码如下:
#include <iostream>
#include <string>
using namespace std;
class Student
{
string name; //Name
string* classList = NULL; //Empty array to store class n