草庐IT

default-constructor

全部标签

c++ - 默认(用户定义)构造函数和带默认参数的构造函数之间的区别?

默认的用户自定义构造函数有什么区别classSimple{public:Simple(){}};和一个用户定义的构造函数,它接受多个参数但每个参数都有默认值classWithDefaults{public:WithDefaults(inti=1){}};除此之外,WithDefaults还可以用i的显式值构造吗?具体来说,我想知道,就语言而言,这两个构造函数是否起到了完全相同的默认构造函数的作用,或者类的属性之间是否存在细微差别?换句话说,所有参数都具有默认值的构造函数是否在所有方面都是默认构造函数? 最佳答案 当前标准工作草案N4

c++ - G++ 4.4.7 中的 "names the constructor, not the type"

我有以下简单的C++代码:#includeclassA{public:A(inty):x(y){}A&operator=(constA&rhs);intx;};A::A&A::operator=(constA&rhs){this->x=rhs.x;return*this;}intmain(int,char**){Aa1(5);Aa2(4);printf("a2.x==%d\n",a2.x);a2=a1;printf("a2.x==%d\n",a2.x);return0;}第11行,A的operator=()函数的定义所在,格式不正确......或者,至少,我相信是这样。正如预期的那样,

c++ - 模板函数 : default construction without copy-constructing in C++

考虑structC{C(){printf("C::C()\n");}C(int){printf("C::C(int)\n");}C(constC&){printf("copy-constructed\n");}};还有一个模板函数templatevoidfoo(){//default-constructatemporaryvariableoftypeT//thisiswhatthequestionisabout.Tt1;//willbeuninitializedfore.g.int,float,...Tt2=T();//willcalldefaultconstructor,thenco

c++ - Pimpl with unique_ptr : Why do I have to move definition of constructor of interface to ".cpp"?

只要我不将构造函数(B)的定义移动到标题B.h中,代码就可以工作。B.hclassImp;//imp;B();//B.cpp#include"B.h"#include"Imp.h"B::B(){}~B::B(){}Imp.hclassImp{};Main.cpp(编译我)#include"B.h"Error:deletionofpointertoincompletetypeError:useofundefinedtype'Imp'C2027我能以某种方式理解必须将析构函数移动到.cpp,因为可能会调用Imp的解构:-deletepointer-of-Imp;//somethinglik

C++ 默认构造函数,用新对象初始化指针

我有以下问题:在myClass中,我想默认初始化一个指向yourClass的指针,并使用新的yourClass地址。不幸的是,如果我想在任何时候删除指针,我都会得到一个(核心转储)。classmyClass{protected:yourClass*yc;public:myClass(){yc=newyourClass();}myClass(yourClass*tyc){deleteyc;yc=tyc;}~myClass(){deleteyc;yc=NULL;}voidsetMyClass(yourClass*tyc){deleteyc;yc=tyc;}voidprint(){yc->p

c++ - 将代码从 C++03 迁移到 C++11 : should I be cautious about the implicit default move constructor?

我有一个代码库,我想从C++03切换到C++11。据我所知,某些类将通过具有隐式默认移动构造函数(以及随之而来的移动赋值运算符)而从更改中受益。虽然我完全同意(我什至认为这是一件好事),但我有点担心这种隐式构造函数可能对我拥有的某些不可复制类产生的影响。我举的一个例子是一个类,它包装了libiconv的iconv_t句柄以利用RAII。更明确地说,类如下:classiconv_wrapper{public:iconv_wrapper():m_iconv(iconv_open()){}~iconv_wrapper(){iconv_close(m_iconv);}private://Not

c++ - 如何在模板元编程中使用 'default' 值

我面临以下问题:我有一些通用容器,能够对类型执行一些操作。为简单起见,这些操作在需要时是线程安全的。并且,请求意味着容器中的类型具有typedefstd::true_typeneeds_thread_safety;。structthread_safe_item{typedefstd::true_typeneeds_thread_safety;/**/};structthread_unsafe_item{typedefstd::false_typeneeds_thread_safety;/**/};templatecontainer{/*somealgorithms,thatarestd

c++ - vector 中元素的默认构造

在阅读此question的答案时我对vector中对象的默认构造有疑问。为了测试它,我编写了以下测试代码:structTest{intm_n;Test();Test(constTest&t);Test&operator=(constTest&t);};Test::Test():m_n(0){}Test::Test(constTest&t){m_n=t.m_n;}Test&Test::operator=(constTest&t){m_n=t.m_n;return*this;}intmain(intargc,char*argv[]){std::vectora(10);for(inti=0;

c++ - 谷歌模拟 : "no appropriate default constructor available"?

将VisualStudio2010C++与googlemock结合使用。我正在尝试使用我创建的模拟,但在线上遇到编译器错误:EmployeeFakeemployeeStub;错误是:1>c:\someclasstests.cpp(22):errorC2512:'MyNamespace::EmployeeFake':noappropriatedefaultconstructoravailable假员工:classEmployeeFake:publicEmployee{public:MOCK_CONST_METHOD0(GetSalary,double());}员工:classEmploy

c++ - 何时使用 =default 与 =delete

据我了解,这些语义仅用于复制构造函数、移动构造函数、复制赋值、移动赋值和析构函数。使用=delete用于禁止使用其中一项功能,即=default如果您想向编译器明确说明在何处使用这些函数的默认值,则使用它。在制作类(class)时使用这些关键字的最佳做法是什么?或者更确切地说,在开发类(class)时我如何记住这些?例如,如果我不知道我是否会使用这些功能之一,最好用delete禁止它。或允许并使用default? 最佳答案 好问题。同样重要的是:哪里使用=default和=delete.我对此有一些有争议的建议。它与我们所有人(包括