我正在尝试初始化一个对象数组:
SinglyLinkedList offeredClasses[22] = {SinglyLinkedList("CSCE101"),SinglyLinkedList("CSCE101L"),SinglyLinkedList("CSCE150E"),SinglyLinkedList("CSCE150EL"),SinglyLinkedList("CSCE150EM"),SinglyLinkedList("CSCE150EML"),SinglyLinkedList("CSCE155"),SinglyLinkedList("CSCE155H"),SinglyLinkedList("CSCE156"),SinglyLinkedList("CSCE230"),SinglyLinkedList("CSCE230L"),SinglyLinkedList("CSCE235"),SinglyLinkedList("CSCE251"),SinglyLinkedList("CSCE310"),SinglyLinkedList("CSCE322"),SinglyLinkedList("CSCE361"),SinglyLinkedList("CSCE351"),SinglyLinkedList("CSCE451"),SinglyLinkedList("CSCE423"),SinglyLinkedList("CSCE428"),SinglyLinkedList("CSCE486"),SinglyLinkedList("CSCE487")};
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这样做时,它一直试图调用我的复制构造函数而不是重载的构造函数.有什么想法解决这个问题?
有问题的2个构造函数是:
SinglyLinkedList(string course); //Constructor
SinglyLinkedList(SinglyLinkedList & otherObj); //Copy Constructor
Run Code Online (Sandbox Code Playgroud)
我需要复制构造函数用于其他事情,所以我不能删除它.
谢谢你的帮助!
这是程序......
class CopyCon
{
public:
char *name;
CopyCon()
{
name = new char;
}
CopyCon(const CopyCon &objCopyCon)
{
name = new char;
_tcscpy(name,objCopyCon.name);
}
~CopyCon()
{
if( name != NULL )
{
delete name;
name = NULL;
}
}
};
int main()
{
CopyCon objCopyCon1;
objCopyCon1.name = "Hai";
CopyCon objCopyCon2(objCopyCon1);
objCopyCon1.name = "Hello";
cout<<objCopyCon2.name<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
一旦代码执行完成,当析构函数调用时,它会在'delete'上崩溃说...
计划:......
HEAP CORRUPTION DETECTED:在正常块(#124)之后的0x00366990.CRT检测到应用程序在堆缓冲区结束后写入内存.
(按"重试"调试应用程序)
我们不必在析构函数中清除堆内存.这个程序有什么问题?请帮忙!复制构造函数按预期完美地工作.但还是......!?
那么为什么不能在“ const Integer operator +(const Integer&rv) ”函数中调用Copy构造函数。是因为RVO。如果是,我需要采取什么措施来防止这种情况发生?
#include <iostream>
using namespace std;
class Integer {
int i;
public:
Integer(int ii = 0) : i(ii) {
cout << "Integer()" << endl;
}
Integer(const Integer &I) {
cout << "Integer(const Integer &)" << endl;
}
~Integer() {
cout << "~Integer()" << endl;
}
const Integer operator+(const Integer &rv) const {
cout << "operator+" << endl;
Integer I(i + rv.i);
I.print();
return I;
}
Integer &operator+=(const Integer &rv) {
cout << "operator+=" …Run Code Online (Sandbox Code Playgroud) 可能重复:
为什么在这种情况下不调用复制构造函数?
在下面的代码中,我构造了三个变量a1,a2和a3.
C++ Primer p.476中有一个例子:
string empty_copy = string();//copy-initialization
Run Code Online (Sandbox Code Playgroud)
有没有人可以帮我解释一下
1)为什么a1和a2不是由复制构造函数构造的
2)我的代码中的初始化a2和书中的empty_copy有什么区别?
非常感谢!
#include<iostream>
using namespace std;
class A{
public:
A(){}
A(int v){}
A(const A&x){
cout<<"copy constructor"<<endl;
}
};
A generateA(){
return A(0);
}
int main(){
cout<<"First:"<<endl;
A a1=generateA();
cout<<"Second:"<<endl;
A a2=A(0);
cout<<"Third:"<<endl;
A a3=a1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是(在Win7中的Visual Studio 2010和Ubuntu10.10中的g ++):
First:
Second:
Third:
copy constructor
Run Code Online (Sandbox Code Playgroud) 可能重复:
如何为成员使用非默认构造函数?
我有当前的代码:
class ImagePoint {
private:
int row;
int col;
public:
ImagePoint(int row, int col){
this->row = row;
this->col = col;
}
int get_row(){
return this->row;
}
int get_col(){
return this->col;
}
};
Run Code Online (Sandbox Code Playgroud)
我想这样做:
class TrainingDataPoint{
private:
ImagePoint point;
public:
TrainingDataPoint(ImagePoint image_point){
this->point = image_point;
}
};
Run Code Online (Sandbox Code Playgroud)
但是这不会编译,因为该行ImagePoint point;要求ImagePoint类具有空构造函数.替代方案(从我读过的)说我应该使用指针:
class TrainingDataPoint{
private:
ImagePoint * point;
public:
TrainingDataPoint(ImagePoint image_point){
this->point = &image_point;
}
};
Run Code Online (Sandbox Code Playgroud)
但是,一旦构造函数完成运行,此指针是否指向已清除的对象?如果是的话,我是否必须复制一份image_point?这需要复制构造函数吗?
我有这样的结构:
/* Renderable definition */
struct Renderable
{
Renderable(VertexBufferPtr vertexBuffer, const Mat4& wvpMatrix, const Mat4& worldMatrix, const Vec4& diffuseColor, const float specularFactor) :
mVertexBuffer(vertexBuffer), mTransform(wvpMatrix, worldMatrix), mMaterial(diffuseColor, specularFactor)
{
}
/* Transform definition */
struct Transform
{
Transform(const Mat4& wvpMatrix, const Mat4& worldMatrix) : mWVPMatrix(wvpMatrix), mWorldMatrix(worldMatrix)
{
}
const Mat4 mWVPMatrix;
const Mat4 mWorldMatrix;
};
/* Material definition */
struct Material
{
Material(const Vec4& diffuseColor, const float specularFactor) : mDiffuseColor(diffuseColor), mSpecularFactor(specularFactor)
{
}
const Vec4 mDiffuseColor;
const float mSpecularFactor;
}; …Run Code Online (Sandbox Code Playgroud) 我写了一个程序如下:
#include <iostream>
using namespace std;
class A {
public:
A() {
}
A(A &a) {
id = a.id;
cout << "copy constructor" << endl;
}
A& operator=(A &other) {
id = other.id;
cout << "copy assignment" << endl;
return *this;
}
A(A &&other) {
id = other.id;
cout << "move constructor" << endl;
}
A& operator=(A &&other) {
id = other.id;
cout << "move assignment" << endl;
return *this;
}
public:
int id = 10;
};
A foo() { …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
#include <iostream>
struct A
{
A() {} // Can't be commented
A( const A& other ) =delete;
int i;
};
struct B : public A
{
B( int j ) { this->i = j; }
B( const B& other ) { this->i = other.i; }
};
int main()
{
B b(42);
std::cout << b.i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我注释A的空构造函数,为什么我不能编译这段代码?我以为A总是有一个默认的构造函数,当我禁用它的复制构造函数时会发生什么变化?
将项目从Qt4迁移到Qt5我得到了这个错误,我已经研究过,显然你不能从QObject创建一个派生类的复制构造函数(这是不可思议的,因为这段代码不是我的,它应该在以前的版本中编译).复制构造函数并不真正复制任何QObject值,我不知道QList thingy有什么问题.
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZArrow]':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32: required from 'void QList<T>::append(const T&) [with T = SIZArrow]'
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:301:49: required from 'void QList<T>::push_back(const T&) [with T = SIZArrow]'
..\marssies\wavingwidget.cpp:345:23: required from here
In file included from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtGui/qwindowdefs.h:45:0,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/qwidget.h:45,
from ..\..\..\..\..\Qt5\5.2.1\mingw48_32\include\QtWidgets/QWidget:1,
from ..\marssies\wavingwidget.h:4,
from ..\marssies\wavingwidget.cpp:1:
..\marssies\graphicsprimitive.h: In instantiation of 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = SIZPolygon]':
..\..\..\..\..\Qt5\5.2.1\mingw48_32\include/QtCore/qlist.h:515:32: required from 'void …Run Code Online (Sandbox Code Playgroud) 有一些宏可以防止类被复制,例如: 宏禁止类复制和赋值.谷歌-vs- Qt
仅仅通过在我班级中使用unique_ptr,我会获得相同的结果吗?如果是这样,有没有理由不这样做?例如
class Foo {
private:
std::unique_ptr<int> DISABLE_COPY;
};
Run Code Online (Sandbox Code Playgroud) c++ ×10
copy-constructor ×10
c++11 ×2
constructor ×2
arrays ×1
const ×1
inheritance ×1
member ×1
migration ×1
qt ×1
unique-ptr ×1