注意:答案是按照特定的顺序给出的,但由于许多用户根据投票而不是给出的时间对答案进行排序,因此这里是答案的索引,它们是最有意义的顺序:
(注意:这是Stack Overflow的C++常见问题解答的一个条目.如果你想批评在这种形式下提供常见问题解答的想法,那么发布所有这些的元数据的发布将是这样做的地方.这个问题在C++聊天室中受到监控,其中FAQ的想法一开始就出现了,所以你的答案很可能被那些提出想法的人阅读.)
我见过default在类中的函数声明旁边使用过.它有什么作用?
class C {
C(const C&) = default;
C(C&&) = default;
C& operator=(const C&) & = default;
C& operator=(C&&) & = default;
virtual ~C() { }
};
Run Code Online (Sandbox Code Playgroud) 我想在编译器通常自动生成默认构造函数,复制构造函数和赋值运算符的条件下刷新内存.
我记得有一些规则,但我不记得了,也无法在网上找到有信誉的资源.有人可以帮忙吗?
c++ copy-constructor default-constructor move-constructor move-assignment-operator
VS2015中以下代码的输出是"构造函数".
由于缺少赋值运算符,它不应该无法编译吗?
struct A { };
struct B {
B(){}
B(const A& a) {
cout << "constructor" << endl;
}
//B& operator=(const A& a) {
// cout << "assignment operator" << endl;
// return *this;
//}
};
int main() {
A a;
B b;
b = a;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 根据cppreference和这个答案,C++应该不自动,如果有一个用户声明析构函数生成一个移动构造函数.但是,在实践中用Clang检查这个,我看到了一个自动生成的移动构造函数.以下代码打印"is_move_constructible:1":
#include <iostream>
#include <type_traits>
struct TestClass
{
~TestClass()
{}
};
int main( int argc, char** argv )
{
std::cout << "is_move_constructible: " << std::is_move_constructible<TestClass>::value << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我误解"没有用户声明的析构函数"或std :: is_move_constructible吗?我正在使用'-std = c ++ 14'和Apple LLVM版本7.0.2(clang-700.1.81)进行编译.
我正在上一堂课,看起来像:
class A {
public:
A(float v)
{
A::v = v;
}
float v;
float set(float v)
{
A::v = v;
return v;
}
float get(float v)
{
return A::v;
}
};
Run Code Online (Sandbox Code Playgroud)
然后,我实例化了A类的2个对象:
A* a = new A(1.0);
A* b = new A(*a);
Run Code Online (Sandbox Code Playgroud)
当我的A类没有采用A类的构造函数时,为什么没有错误?
在Bjarne Stroustrup的“ C ++编程语言(第4版)”的第17.6节(生成默认操作)中,提到了这一点:
如果程序员声明了某个类的复制操作,移动操作或析构函数,则不会为该类生成任何复制操作,移动操作或析构函数。
因此,我很困惑为什么SubObj在此程序中调用析构函数:
#include <iostream>
using namespace std;
class SubObj {
public:
~SubObj() {
cout << "SubObj Destructor called" << endl;
}
};
class Obj {
private:
SubObj so;
public:
Obj() {};
Obj(const Obj& o) {};
};
int main() {
Obj();
cout << "Program end" << endl;
}
Run Code Online (Sandbox Code Playgroud)
使用g ++编译时,我得到以下输出:
$ ./a.out
SubObj Destructor called
Program end
Run Code Online (Sandbox Code Playgroud)
根据我的理解,我希望默认的析构函数Obj不会自动生成,因为我为定义了复制操作Obj。因此,我希望不会破坏的SubObj成员,Obj因为没有的析构函数Obj。
因此,我想知道:即使没有析构函数,对象成员也会自动销毁吗?还是为此示例自动生成了析构函数?
编辑:
在本书的稍后部分(17.6.3.4)中,当提到一个示例时,Bjarne提到:
我们定义了副本分配,因此我们还必须定义析构函数。该析构函数的原因可能是
=default,它所要做的就是确保成员pos …
有人可以解释为什么以下工作,尝试以下代码,它工作正常.
class A {
public :
int32_t temp ;
A ( bool y = false ) { }
} ;
int main ( int argc, char *argv[] )
{
A temp ;
temp = new A () ;
temp.temp = 5 ;
std::cout << " " << temp.temp << std::endl ;
return EXIT_SUCCESS;
} // ---------- end of function main ----------
Run Code Online (Sandbox Code Playgroud) 我正在尝试确定C++函数是否安全返回具有构造函数和析构函数的对象.我对标准的理解是它应该是可能的,但我用简单的例子进行的测试表明它可能有问题.例如以下程序:
#include <iostream>
using namespace std;
struct My
{ My() { cout << "My constructor " << endl; }
~My() { cout << "My destructor " << endl; }
};
My function() { My my; cout << "My function" << endl; return my; }
int main()
{ My my = function();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
给出输出:
My constructor
My function
My destructor
My destructor
Run Code Online (Sandbox Code Playgroud)
在MSVC++上编译时,但使用gcc编译时会得到以下输出:
My constructor
My function
My destructor
Run Code Online (Sandbox Code Playgroud)
这是"未定义的行为"的情况,还是一个不按标准方式运行的编译器?如果是后者,哪个?gcc输出更接近我的预期.
到目前为止,我一直在设计我的类,假设每个构造函数调用最多只有一个析构函数调用,但是这个例子似乎表明这个假设并不总是成立,并且可能依赖于编译器.标准中是否有任何内容指定此处应该发生什么,或者最好避免函数返回非平凡对象?如果这个问题是重复的,请道歉.
我写了下面的c ++代码试图理解c ++中的copy elision.
#include <iostream>
using namespace std;
class B
{
public:
B(int x ) //default constructor
{
cout << "Constructor called" << endl;
}
B(const B &b) //copy constructor
{
cout << "Copy constructor called" << endl;
}
};
int main()
{
B ob =5;
ob=6;
ob=7;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
Constructor called
Constructor called
Constructor called
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么构造函数被调用三次,每次赋值给对象ob.
c++ ×11
destructor ×3
c++11 ×2
constructor ×2
c++-faq ×1
c++14 ×1
declaration ×1
default ×1
keyword ×1
operators ×1
overloading ×1
pointers ×1