我注意到我通常使用常量引用作为返回值或参数.我认为原因是它与在代码中使用非引用几乎相同.但它肯定需要更多的空间和功能声明变得更长.我很喜欢这样的代码,但我认为有些人发现它编程风格很糟糕.
你怎么看?是否值得编写const int& over int?我认为无论如何它都是由编译器优化的,所以也许我只是在浪费时间编码,一个?
以下代码仅在复制构造函数可用时有效.
当我添加print语句(via std::cout)并使复制构造函数可用时,它不会被使用(我假设有这样的编译器技巧,以删除不必要的副本).
但是在输出operator <<和plop()下面的函数(我创建一个临时对象)中我都没有看到复制构造函数的需要.当我通过const引用(或者我做错了)传递所有内容时,有人可以解释为什么语言需要它.
#include <iostream>
class N
{
public:
N(int) {}
private:
N(N const&);
};
std::ostream& operator<<(std::ostream& str,N const& data)
{
return str << "N\n";
}
void plop(std::ostream& str,N const& data)
{
str << "N\n";
}
int main()
{
std::cout << N(1); // Needs copy constructor (line 25)
plop(std::cout,N(1)); // Needs copy constructor
N a(5);
std::cout << a;
plop(std::cout,a);
}
Run Code Online (Sandbox Code Playgroud)
编译:
[Alpha:〜/ X] myork%g ++ -v
使用内置规格.
目标:i686-apple-darwin10
配置:/ var/tmp/gcc/gcc-5646~6/src/configure --disable-checking --enable-werror --prefix …
#include <iostream>
class A {
public:
A(){ cerr << "A Constructor" << endl; }
~A(){ cerr << "A Destructor" << endl; }
A(const A &o){ cerr << "A Copy" << endl; }
A& operator=(const A &o){ cerr << "A Assignment" << endl; return *this; }
};
class B : public A {
public:
B() : A() { cerr << "B Constructor" << endl; }
~B(){ cerr << "B Destructor" << endl; }
private:
B(const B &o) : A() { cerr …Run Code Online (Sandbox Code Playgroud) 我正在阅读直接初始化和复制初始化(§8.5/ 12)之间的区别:
T x(a); //direct-initialization
T y = a; //copy-initialization
Run Code Online (Sandbox Code Playgroud)
我从阅读有关复制初始化的内容中了解到,它需要可访问和非显式的复制构造函数,否则程序将无法编译.我通过编写以下代码验证了它:
struct A
{
int i;
A(int i) : i(i) { std::cout << " A(int i)" << std::endl; }
private:
A(const A &a) { std::cout << " A(const A &)" << std::endl; }
};
int main() {
A a = 10; //error - copy-ctor is private!
}
Run Code Online (Sandbox Code Playgroud)
GCC给出了一个错误(ideone)说:
prog.cpp:8:错误:'A :: A(const A&)'是私有的
到目前为止,一切都很好,重申Herb Sutter所说的,
复制初始化意味着在必要时首次调用用户定义的转换后,使用复制构造函数初始化对象,并且等效于"T t = u;"形式:
之后,我通过评论private …
以下代码不能在Visual C++ 2008和2010上编译:
#include <memory>
struct A {};
std::auto_ptr<A> foo() { return std::auto_ptr<A>(new A); }
const std::auto_ptr<A> bar() { return std::auto_ptr<A>(new A); }
int main()
{
const std::auto_ptr<A> & a = foo(); // most important const
const std::auto_ptr<A> & b = bar(); // error C2558:
// class 'std::auto_ptr<_Ty>' :
// no copy constructor available or copy
// constructor is declared 'explicit'
bar(); // No error?
}
Run Code Online (Sandbox Code Playgroud)
我期望"最重要的const"应用于变量"b",然而,它不会编译,并且由于某种原因,编译器要求复制构造函数(这让我感到惊讶,因为这里不应该涉及复制) .独立调用bar()工作正常,这意味着,我猜,它实际上是初始化b的问题.
这是编译器错误,还是标准中描述的真正的编译错误?
(也许它在C++ 98中被禁止并在C++ 11中被授权?)
注意:它在Visual C++ 2012,gcc 4.6和Solaris …
class test{
public:
int data;
test(const test& ){cout<<"INSIDE COPY CON "<<endl;}
test(int val = 0) : data(val){ cout<<"INSIDE CON "<<endl; }
test testfun(const test& obj)
{
cout<<"data : "<<data<<endl;
//test test3(this->data + obj.data);
//cout<<"test3 :"<<test3.data<<endl;
//return test3; //This will work only if return type is changed to const ref
return test(data + obj.data);
}
};
int main()
{
test testO1(1);
test testO2(2);
test testO3 = testO1.testfun(testO2);
cout<<testO3.data<<endl;
getchar();
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT:
INSIDE CON
INSIDE CON
data : 1
INSIDE CON
3 …Run Code Online (Sandbox Code Playgroud) c++ ×6
auto-ptr ×1
c++11 ×1
c++98 ×1
coding-style ×1
const ×1
gcc4 ×1
reference ×1
visual-c++ ×1