我不明白为什么在这种情况下不会调用复制构造函数.有人可以解释一下吗?
#include <iostream>
class foo
{
int* ptr;
public:
foo()
{
std::cout << "\n Constructor \n" ;
ptr = new int;
*ptr = 10;
}
foo( const foo* &obj ) // Copy Constructor
{
std::cout << "\n Copy Constructor \n" ;
ptr = new int;
*(this->ptr) = *(obj->ptr);
}
// Copy Assignment Operator
~foo() // Destructor
{
delete ptr;
}
};
int main()
{
foo* objOne = new foo;
foo* objTwo = objOne ;
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我在使用自定义类的容器时遇到问题.每次我尝试创建一个对象并推送它时,推送对象中的成员变量显示为零.因此,myConnection.value显示为0而不是其值.我怀疑我的拷贝构造函数有问题.
全球定义
vector<connection> Connections;
Run Code Online (Sandbox Code Playgroud)
麻烦的地方
while ( dataInput.good() )
{
getline (dataInput,line);
sscanf(line.c_str(),"%lg %lg %d %d",&k,&order,&final,&initial);
printf("%lg %lg %d %d\n",k,order,final,initial);//Looks Good
Connections.push_back(connection(k,order,final,initial));
printf("%lg %lg %s %d\n",Connections[i].k,Connections[i].order,Connections[i].to,Connections[i].from);//Everything is zero!?
i++;
}
Run Code Online (Sandbox Code Playgroud)
Connection.cpp
#include "../include/connection.h"
#include <stdio.h>
connection::connection(double kin, double orderin, int fromin,int toin)
{
k=kin;
order=orderin;
from = fromin;
to=toin;
printf("%d %d %i %i\n",kin,orderin,fromin,toin);
}
connection::~connection()
{
//dtor
}
connection::connection(const connection &c)
{
connection(c.k,c.order,c.from,c.to);
}
Run Code Online (Sandbox Code Playgroud)
Connection.h
#ifndef CONNECTION_H
#define CONNECTION_H
class connection
{
public:
connection(double, double, int,int);
connection(const connection& c);
virtual …Run Code Online (Sandbox Code Playgroud) 我知道这个交易.编译器尝试在转换运算符的帮助下将一个对象转换为其他对象的类型.有两种方法可以做到这一点.Constructor(将类转换为另一个)或转换运算符.所以,这个只是为了测试我是否彻底了解了这些概念.下面的代码给出了错误
using namespace std ;
class A
{
int i ;
public:
A(int a=0){this->i=a;}
A operator+(const A& b){
A c ;
return c(this->i+b.i);
}
void show()
{
cout<<i<<endl;
}
};
int main()
{
A a1(1),a2(2),a3;
a3=a2+a1;
a3.show();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我猜错误是在运算符+.当我尝试分配A(i).对于可以从int创建A的运算符没有匹配.
但后来我看到这个A的构造函数潜伏在后面.它可以将一个int转换为一个.Suppose,它确实将int转换为A.然后,调用变为A(B).这相当于复制构造函数.Hence,this电话应该工作.但它没有.总而言之,我很困惑.
请帮忙 .
考虑以下 :
class A
{
public:
int xx;
A(const A& other)
{
cout << "A cctor" << endl;
/* do some stuff */
}
A(int x) : xx(x) {} /* conversion constructor */
};
int main()
{
A a = 1;
A other = a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下(以及一般情况下)说CCtor从const转换为非const是正确的吗?
谢谢,罗恩
我正在用C++编写一个智能指针实现,而且我在使用const-correctness方面遇到了一些麻烦.以下是代码的摘录:
template <class T> class Pointer {
T* pointee;
public:
Pointer(const Pointer<T>& other) { // must be const Pointer& for assignments
pointee = other.getPointee();
}
T* getPointee() const {
return pointee;
}
};
Run Code Online (Sandbox Code Playgroud)
这是一种方法,但是我感到不安的是const会员没有返回const指针.另一种可能性是getPointee()返回a const T*并const_cast<T*>在复制构造函数中执行a .
有没有更好的方法呢?如果没有,你认为哪个是较小的邪恶,返回一个非常数或做一个const_cast?
我知道当你创建一个ArrayList并且在声明它时你将它引用到另一个ArrayList它只引用另一个,所以对第二个引起的更改改变了第一个.但是面对这个问题我很困惑.
ArrayList <Productos> d3 = abd.dadesProductos();
ArrayList <Productos> dades2 = new ArrayList <Productos>();
System.out.println("before clear() + size= "+d3.get(i).configurables.size());//43
dades2.add(d3.get(i));
dades2.get(dades2.size()-1).configurables.clear();
System.out.println("after clear() + size= "+d3.get(i).configurables.size());//0
Run Code Online (Sandbox Code Playgroud)
问题是,在从添加到dades2的最后一项中清除参数配置(另一个arraylist)后,它也会从d3清除它,我不希望这种情况发生...为什么如果我创建一个ArrayList就会发生这种情况如 new ArrayList <Productos>();
我会提供任何帮助.
一些额外的信息.
我试图像这样创建一个新的构造函数:
Productos(Productos p)
{
this(p.entity_id, p.model, p.sku, p.name, p.weight, p.visibility, p.material, p.attribute_set_name, p.image, p.category_ids, p.category_ids2, p.color, p.color2, p.color3, p.cpsp_enable, p.created_at, p.description, p.colorswatch_disabled_category, p.colorswatch_disabled_product, p.msrp_display_actual_price_type, p.options_container, p.fcpm_enable, p.is_recurring, p.image_label, p.manage_stock, p.manufacturer, p.max_sale_qty, p.meta_description, p.meta_keyword, p.meta_title, p.category_positions, p.price, p.type_id, p.fcpm_second_attribute, p.fcpm_template, p.fcpm_template_position, p.short_description, p.fcpm_showfdd, p.fcpm_show_grandtotal, p.fcpm_second_layout, p.fcpm_show_link, p.fcpm_checkbox, p.fcpm_show_image, p.fcpm_show_rowtotal, p.fcpm_show_stock, …Run Code Online (Sandbox Code Playgroud) 我有一段这样的代码:
class EducationalCentre
{
string _centreName;
vector<Course> _courses; // courses offered by the Centre
Collection<Course*, Student*, 150> _applicants;
public:
EducationalCentre(string name="<name>")
{
_centreName = name;
}
EducationalCentre(const EducationalCentre& obj)
:_courses(obj._courses), _applicants(obj._applicants)
{
_centreName = obj._centreName;
}
};
Run Code Online (Sandbox Code Playgroud)
现在,在这部分_applicants(obj._applicants)复制构造标题中,周围有一条波浪形的红线(obj,将其悬停在错误上,说明类型不兼容(const正在提及).
因为我不想在这个阶段改变任何东西(这是考试测试的一部分) - 我想知道为什么会发生这种情况.
我试图删除const从EducationalCentre(const EducationalCentre& obj),这确实解决了问题,但..正如我所说,我宁愿了解是什么导致这而不是删除它.
我有以下代码:
template<class T = char>
class String
{
public:
// Default constructor
String()
: buffer(nullptr),
len(0)
{
cout << "Default constructor" << endl;
}
// Constructor
String(const char* s)
{
cout << "Constructor (const char*)" << endl;
//...
}
// Virtual destructor.
virtual ~String()
{
cout << "Destructor" << endl;
len = 0;
delete[] buffer;
}
// Copy constructor
String(const String& s)
{
cout << "Copy constructor" << endl;
buffer = new T[s.len];
std::copy(s.buffer, s.buffer + s.len, buffer);
len = …Run Code Online (Sandbox Code Playgroud) 在旧版本的C++如果有人想阻止copying一个类的对象,他们通常会宣布Copy Constructor和operator=()一类这样的民营部分:
class Foo {
public:
Foo(){} // Default
~Foo(){} // Default
private:
Foo( const Foo& c ); // not implemented
Foo& operator=( const Foo& c ); // not implemented
};
Run Code Online (Sandbox Code Playgroud)
这很直截了当.现在有了更新版本的C++,我们现在可以做到这一点.
class Foo {
public:
Foo() = default;
~Foo() = default;
private:
Foo( const Foo& c ) = delete;
Foo& operator=( const Foo& c ) = delete;
};
Run Code Online (Sandbox Code Playgroud)
我的问题就变成了这样:用现代方法将这些声明为deleted函数,如果它们是在类的公共部分,受保护部分或私有部分中定义的,那么它是否重要或有什么不同?
例如,上面的一个和这个有什么区别:
class Foo {
public:
Foo() = default;
~Foo() = default;
Foo( …Run Code Online (Sandbox Code Playgroud) 下面是我使用删除的副本构造函数和副本分配运算符定义的类。这是唯一必须做的假设。
class MyClass
{
public:
explicit MyClass(int i) : i(i) {}
MyClass(const MyClass&) = delete;
MyClass& operator=(const MyClass&) = delete;
MyClass(MyClass&& other) :
i(std::move(other.i))
{}
MyClass& operator=(MyClass&& other) {
i = std::move(other.i);
return *this;
}
private:
int i;
};
Run Code Online (Sandbox Code Playgroud)
然后的目标是在编译时将我的类添加到std :: vector。
int main()
{
std::vector<MyClass> v{MyClass{0}, MyClass{1}, MyClass{2}};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的编译器告诉我STL需要使用已删除的副本构造函数,MyClass::MyClass(const MyClass&)但是有什么解决办法吗?
我已经知道在运行时添加值的一种可能方法,但是我认为以下方法是一个较差的解决方案,因为我丢失了编译时间检查。
int main()
{
std::vector<MyClass> v;
v.emplace_back(MyClass{0});
v.emplace_back(MyClass{1});
v.emplace_back(MyClass{2});
return 0;
}
Run Code Online (Sandbox Code Playgroud)