D*****r 发帖数: 6791 | 1 http://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html
High School/Jr.High
10 PRINT "HELLO WORLD"
20 END
First year in College
program Hello(input, output)
begin
writeln('Hello World')
end.
Senior year in College
(defun hello
(print
(cons 'Hello (list 'World))))
New professional
#include
void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}
... 阅读全帖 |
|
f*****Q 发帖数: 1912 | 2 整了个类似ObjectiveC学习笔记的东西,发上来大伙看看有兴趣不。
修改了一点,增加了NSAutoreleasePool的内容。
增加了NSString内容。
===========俺系分隔线==================
本文假设读者有基本的C编程能力,如果有C++或者Java的背景会更容易理解但是不是必须。
ObjectiveC基本语法
消息
在objectiveC中,向一个对象发送一个消息的语法为
[ obj method:parameter];
类似的功能在C++中写作
obj->method(parameter);
在java中写作
obj.method(parameter);
在smalltalk中写作
obj method:parameter
显而易见objectiveC和smalltalk的语法基本是相同的。
当有两个或者两个以上的参数时,通常试用以的语法
[ obj method:parameter1 WithSecondParameter:parameter2];
定义一个类的代码放在一个.h文件中,下面是一个例子。
//macdevexample1.h
... 阅读全帖 |
|
u*********r 发帖数: 2735 | 3 this statement is wrong: "const的reference只能指向const变量"
const reference only means the reference itself can not be used to modify
what it refers to. the target does not have to be const.
try this:
int i = 0;
const int& x = i;
std::cout << x << std::endl;
i = 4;
std::cout << x << std::endl;
// x = 6; // compiling error |
|
vi 发帖数: 309 | 4 Although I vaguely know the differences between private and protected,
but I am not sure on exactly where should I use which. For an example,
STL list is declared as:
template >
class stack {
public:
bool empty() const;
size_type size() const;
value_type& top();
const value_type& top() const;
void push(const value_type& val);
void pop();
protected:
Container c;
};
Question: why 'protected' is used here?
Thanks! |
|
K*****n 发帖数: 65 | 5 when a member function is called, "this" pointer is implicitly passed as
argument.It is part of function signature. Therefore
void func() const;//say "*this" will not be changed.
void func(); //"*this" may or may not be changed.
void main()
{
test a;
a.func(); //call void func();
((const A)a).func(); //call void func() const;
(*(const A *)&a).func(); //call void func() const;
} |
|
n*******s 发帖数: 482 | 6 恩 仔细看了一下,是我的测试代码写错了。
String s(" hello world ");
cout<
const char* o = (const char*)s;
cout<<"const char* o="<
char* o1 = (char*)o;
cout<<"char* o1="<
remove_blanks(o1);
cout<<"remove_blanks(const char* -> char*)o1="<
结果没啥问题。
我想对于这种Const char* --> char*的转换,如果后面对char*的操作是变短 就没
啥问题,如果变长 可能会覆盖其他变量吧。
谢谢observer RP++
:) |
|
s****y 发帖数: 4 | 7 thanks for the answers, to summarize,
const char *p = const char* p = char const *p = char const* p
a pointer to constant object
char* const p
a constant pointer to an object
is this right? |
|
f*******y 发帖数: 988 | 8 string literal比较tricky的地方就是他们的真正类型其实是const char []
这个通过了,因为你的编译器是先算 operator + (std::string, std::string),
const char [8] 被cast成std::string 了
这个不行是因为 operator + (const char [6], const char [8]) 不存在
你搞个const std::string message = hello + (", world" + "!");就不行 |
|
i**p 发帖数: 902 | 9 How can I define a reference to ensure it will always reference the same
object but you can use the reference to modify the object's values?
The follow FAQ says a reference is always const. Do you agree it?
http://www.parashift.com/c++-faq-lite/const-correctness.html
...
[18.7] Does "Fred& const x" make any sense?
No, it is nonsense.
To find out what the above declaration means, you have to read it right-to-
left. Thus "Fred& const x" means "x is a const reference to a Fred". But
that is redunda |
|
x*****n 发帖数: 3422 | 10 请看看哪里有问题?
// temp.cpp, test stl iterator
#include
#include
#include
using namespace std;
template void print(const set& mySet, const string& name)
{
set::const_iterator i;
}
int main()
{
}
// end of file
cygwin下g++ 3.4.4:
$ g++ temp.cpp
temp.cpp: In function `void print(const std::s
tor<_CharT> >&, const std::string&)':
temp.cpp:11: error: expected `;' before "i"
linux下g++ 3.2.3:
temp.cpp: In function `void print(const std::set,
|
|
h****s 发帖数: 29 | 11 希望用一个const 对象的地址来初始化一个引用
const int ival = 1024; // 1
const int *&pi_ref = &ival; // 2
const int *const &pi_ref = &ival; //
书上看来的,为什么第2行代码是错的,而第3行代码是对的,百思不得其解啊,多谢解释 |
|
P********e 发帖数: 2610 | 12 google can help on those issues:
#include
string();
string( const string& s );
string( size_type length, const char& ch );
string( const char* str );
string( const char* str, size_type length );
string( const string& str, size_type index, size_type length );
string( input_iterator start, input_iterator end );
~string();
string doesn't take any char to initialize
so, do this:
string b = "";
b.append(0,a.at(0));
不好意思,是我写问题的时候打错了
应该是
string A="God";
string B=A.at(0);
编译时 |
|
r****t 发帖数: 10904 | 13 有这么一段 code,
#define NO_EXCEPTIONS
namespace MyNamespace {
class Location
{
public:
Location(const char *name);
Location() NO_EXCEPTIONS;
Location(const Location &);
Location & operator=(const Location & loc);
template
void read(const char *dataset_name, T & matrix) const {
MyNamespace::read(*this, dataset_name, matrix); //<<<<<< ERROR!
// Refer it to a non-member function
// so it can be overloaded even for classes
// |
|
i***h 发帖数: 12655 | 14 在Effective STL 书里看到的, 但是不太清楚.
为什么在第二段code里可以直接用DeleteObject,
而第一个里面一定要DeleteObject呢?
第一段Code:
template
struct DeleteObject:
public unary_function {
void operator()(const T* ptr) const
delete ptr;
}
};
void doSomething()
{
for_each(vwp.begin(), vwp.end(), DeleteObject);
}
第二段Code:
struct DeleteObject { // templatization and base
// class removed here
template II templatization added here
void operator()(const T* ptr) const
{
delete ptr;
}
} |
|
k***n 发帖数: 20 | 15 class A {
private:
...
public:
A();
A(const char x, const char y);
A(const string& z);
...
}
int main () {
A aa;
const string str("AT");
aa(str);
return 0;
}
gcc编译提示:
no match for call to `(A) (const std::string&)'
我知道这个错误可能很低级,不吝求教。 |
|
H*M 发帖数: 1268 | 16 class X
{
public:
X& operator=(const X& rhs);
const X& operator+(const X& rhs) const;
const X& operator+(int m);
private:
int n;
};
int main()
{
X a,b,c;
a=a=b+c;
a=a+5+c;
a=b+c+5; //wrong here..why?
a=b+5;
(c=a+a)=b+c;
} |
|
b********r 发帖数: 1080 | 17
我的理解是a=a+5+c先执行a+5, 返回的是const&, 也就是a本身.但这个const a
的operator + 在做+c 时企图改变*this的值,所以不对.
我觉得应该改成
X& operator+(const X& rhs);
X& operator+(const int m);
才对.就是去掉前后的const. |
|
X****r 发帖数: 3557 | 18 qsort passes pointers to the objects being compared. Your objects
in the array are "char *", thus the comparison function parameters
have actual type "char **".
For example, if you're sorting integers, you would do:
int myintcmp(const void *p1, const void *p2) {
return *(const int*)p1 - *(const int *)p2;
}
int data[] = {1,3,2,4};
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), myintcmp);
Just replace "int" in above example with "char *" you'll see.
const*)str2); |
|
t****t 发帖数: 6806 | 19 The synopsis of std::stack<> is shown below. Therefore, your Container
should satisfy:
typename Container::value_type;
typename Container::size_type;
Container::empty() const;
Container::size() const;
Container::top();
Container::top() const;
Container::push_back(const value_type&);
Container::pop_back();
namespace std {
template |
|
h****e 发帖数: 2125 | 20
why can't? try this:
"
#include
class X
{
public:
X* getInstance() { return &_x; }
void setMember( int i ) { _i = i; }
int getMember() const { return _i; }
const int* getMemberAddr() const { return (long long)&_i; }
private:
X() {}
~X() {}
X( const X& );
X& operator=( const X& );
static X _x;
int _i;
};
X X::_x;
int main()
{
X* p;
std::cout << p->getInstance() << std::endl;
p->getInstance()->setMember( 10 );
... 阅读全帖 |
|
r*******y 发帖数: 1081 | 21 I use g++
I tried this. If M is a class and given with M(int) and M(const M &)
if I write
M mm = 1;
then my experiment told that only M(int) is
called and M(const M &) is not called.
But if I write
M cc(1);
M nn = cc;
then M(const M &) is called in M nn=cc
Also as in my post at the beginning, I will fail to compile
M cc = 1;
if only M(M &) defined instead of M(const M&).
I read the book c++ primer and seems to get something there.
Let's look at M cc = 1. first a temporary object is initilized
w... 阅读全帖 |
|
c********2 发帖数: 56 | 22 1)
class foo{
public:
foo(int i){}
};
class bar: virtual foo{
public:
bar(){}
};
bar b;
what's wrong with the above code
2)
class X{
public:
X& operator=(const X& rhs);
const X& operator+(const X&rhs) const;
const X& operator+(int m);
private:
int n;
};
int main(){
X a, b, c;
system("pause");
return 0;
}
which one is wrong?
a=c+5;
a=a=b+c;
(a=c+4)=b+2;
a=b+c;
b=a+1;
3)
template<> class Stack
{
public:
int push(char);
char pop();
};
what template<> signify in the... 阅读全帖 |
|
r*******y 发帖数: 1081 | 23 class A{
int i;
public:
A(int ii):i(ii){}
const A operator+(const A & a){
A b(i + a.i);
cout << "member"<
return b;
}
friend const A operator +(const A &a, const A & b){
cout <<"friend"<
return A(a.i + b.i);
}
};
int main(){
A a(1);
a + 1; // will call member operator +
}
This example shows that the member operator + will be called in
a + 1 a... 阅读全帖 |
|
A**u 发帖数: 2458 | 24 小弟在读effective c++, 第31节
讲到interface class, 里面提到 factory function, 有些不明白, 请大家指点
下面是代码.
interface class
class Person{
public:
static std::tr1::shared_ptr create(const std::string& name);
virtual ~Person();
virtual std::string name() const = 0;
}
这里的create 就是书里说的factory 函数
class RealPerson: public Person{
public:
RealPerson(const std::string& name):thename(name){}
virtual ~RealPerson();
std::string name() const;
private:
std::string the name;
}
奇怪的是 create 实现为
std::... 阅读全帖 |
|
t****t 发帖数: 6806 | 25 18.4 Dynamic memory management [lib.support.dynamic]
1 The header defines several functions that manage the allocation of
dynamic storage in a program.
It also defines components for reporting storage management errors.
Header synopsis
namespace std {
class bad_alloc;
struct nothrow_t {};
extern const nothrow_t nothrow;
typedef void (*new_handler)();
new_handler set_new_handler(new_handler new_p) throw();
}
void* operator new(std::size_t size) throw(std::bad_alloc);
void* operator ne... 阅读全帖 |
|
g*****1 发帖数: 998 | 26 class base{};
class derived{};
class derived2: public base, derived {};
which is true?
1) This is illegal
2) derived2 is derived public from base and private from derived
3) derived2 is derived public from base and public from derived
4) derived2 is derived public from base and protected from derived
class X{
public:
X& operator=(const X& rhs);
const X& operator+(const X&rhs) const;
const X& operator+(int m);
private:
int n;
};
int main(){
X a, b, c;
system("pause");
... 阅读全帖 |
|
x******a 发帖数: 6336 | 27 请问这个mergesort有什么问题?编译时没问题, 执行时提示Segmentation fault 11
多谢。
#include
using namespace std;
void merge(int a[], int const left, int const mid, int const right)
{
int* b=new int[right-left];
int h,i,j,k;
h=left;
i=0;
j=mid;
//merges the two array's into b[] till the first is finished
while((h
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
//completes the array filling i... 阅读全帖 |
|
t****t 发帖数: 6806 | 28 const char* <===> char const* (pointer to const char)
char* const (const pointer to char)
e. |
|
r**a 发帖数: 536 | 29 下面的code中,输出为"Constructor, Constructor, Destructor, 3" 前两个
constructors分别是create b1 and b2是出现的。第3个Destructor应该是对应到“b1
= b2;"。我的问题是此时b2已经被free掉了,为啥最后的结果是3呢?哪位大牛给解释
解释。谢谢了。
#include
#include
#include
#include
class StrBlob
{
public:
typedef std::vector::size_type size_type;
StrBlob();
StrBlob(std::vector &il);
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_... 阅读全帖 |
|
t****t 发帖数: 6806 | 30 你和他错得差不多, 大哥就不要说二哥了.
首先, 这些operator, 除了"="及其派生("+=", "-=", 等), 几乎都是非成员模板. 所
以完全没有你们说的这些问题. 直接写
bool operator==(const iterator&, const iterator&)
就搞定了. 当然实际的代码里还有些修饰.
其次, 就算是写成员, 成员本身也必须是const, 比如
bool A::operator==(const A&) const; |
|
s*****n 发帖数: 5488 | 31 因为有了指针,可以随便改来该去。好吧,解决它,我们来定义const.
一堆const. non-const.你说把指针去了不就是完事大吉了?
const又引出一堆的问题,const-cast啊。
然后指针导致无法正确的回收,都是一堆的解决方案,smart pointer了什么啊。
所以我说,做加法永远不如做减法,咔嚓一刀,指针切掉,世界安静了。 |
|
D***n 发帖数: 6804 | 32 你需要知道编译器在看到你这段话的时候真正干了啥。
1)当编译器看到const的时候,它知道这个东西是不会改变的。
2)当编译器看到static的时候,它知道要把这个东西放到一个特殊的内存区域,并且
在程序载入的时候初始化(理解这点特别重要)。
一个东西又是const又是static,理论上来说,应该在内存静态变量区创建一个变量,
把具体数字塞进去,然后其他人在访问结构的时候可以通过地址访问它....
但是实际上,编译器会在相应的struct的代码段上直接计算出常量并且写进去(内存访
问两次跳跃),一个在代码段的常量当然无法赋值。
你那个程序的main实际上是这样的:
int main() {
A test;
int a=10;
10=15; /* 错误 */
}
理解了这一点你可以省掉那些 C++书上一万个blah, blah, blah
f()
你的f().x是lvalue
看看我的const static int 是不是rvalue
不能进行test.y的赋值
我不知道是因为const还是因为rvalue
struct A {
const static int y=... 阅读全帖 |
|
m*********a 发帖数: 3299 | 33 如果有MyClass
foo(MyClass())就会调用foo(MyClass&&)
MyClass()产生一个新的unamed object
如果先定义一个
MyClass myClass;
foo(myClass)就是调用foo(MyClass&)或者foo(const MyClass &)
foo(MyClass&)和foo(const MyClass &)虽然signature 不同
但是不能overload.
虽然const这儿是low level modifier, 但是const low level可以接受nonconst 和
const的variable |
|
x****k 发帖数: 2932 | 34 在g++下试了下,第一次是生成
const A &a2 = A()
第二次是生成
const A &a1
帮你改了一下,会更加说明问题
#include
class A
{
public:
A(int n = 0)
: m_n(n)
{
std::cout << n << "n";
}
A(const A& a)
: m_n(a.m_n)
{
std::cout << 'c';
}
private:
int m_n;
};
void f(const A &a1, const A &a2 = A(99))
{
}
int main()
{
f(3);
return 0;
} |
|
|
s******e 发帖数: 2181 | 36 首先我有一些基本的问题想请教大拿,Nvidia的手册里找不到答案,sample code太简
单也太少。
1,多GPU并行的情况下,我要从GPU0拷贝数据到GPU1,使用cudaMemcpy()必须要在当
前选中的GPU1下执行么?还是0和1都可以?
2,我使用了SPMD并行模式,每一个计算机核下挂了一个GPU,照理来说数据都是独立的
,各个GPU内的同名变量其实数据不同且相互不可见,但如果是在unified address
space下呢,这些相同变量名的变量相互冲突吗?
我在matlab环境下用mexfunction编写的cuda,主程序是一个matlab program是一个
SPMD结构,SPMD结构里面调用mexfunction来实现GPU0内的数据传给GPU1,GPU1内的数
据传给GPU2。请高手指点哪里出了问题。MathWorks的技术客服远程登陆到我电脑上
debug两个小时没能解决问题。
spmd(3)
if labindex==1
A=gpuArray(zeros*(1));
elseif labindex==2
... 阅读全帖 |
|
h******6 发帖数: 5 | 37 本来矩阵向量运算的code都是自己乱写的, 现在想学用一下 blas lapack 这样的标准
库。 所以先找了一段 ATLAS 的源代码, 不是很懂, 有牛人能解释下下面的这段 代
码么? 特别是为什么 Mjoin 函数名后面能跟两个参数列表? 多谢!
#include "atlas_misc.h"
#include "atlas_level1.h"
#ifdef TREAL
void Mjoin(PATL,rot)(const int N, TYPE *X, const int incX,
TYPE *Y, const int incY, const TYPE c, const TYPE s)
{
int i;
TYPE tmp;
if (c != ATL_rone || s != ATL_rzero)
{
if (incX == 1 && incY == 1)
{
for (i=0; i != N; i++)
{
tmp = c * |
|
m*****n 发帖数: 5245 | 38 ☆─────────────────────────────────────☆
lensbo (l****[email protected]) 于 (Fri Jan 30 16:03:15 2009) 提到:
onsite 只见了2个人,估计over了,总结一下教训。
题目不难,主要一共3道题,都算比较基础的题。
1.reverse words in a sentence,使用如下函数。
char* reverseWord(const char* str)
2.an interger array containing millions of elements with min 0 and max 1000,
how to sort it?
3.covert interger number to date string, for example, 20090130 -> "01/30/
2009"
说说教训:
第一道被输入const给搞死了。先是没有注意const,直接按照常规非const做,没有写完
就被叫停了;然后是被平时强调的malloc后必须及时delete规则搞死,坚持认为在函数 |
|
a**********s 发帖数: 588 | 39 不好意思, 我没有写好, 刚才写了一个, 你可以copy&paste一下看看, 当然, 上面几楼
说的直接推导的公式是最干净利落的
template
struct Mat2X2
{
T m[4];
T* operator [] (int i) { return m + i*2; }
const T* operator [] (int i) const { return m + i*2; }
Mat2X2 operator * (const Mat2X2& op) const {
Mat2X2 M = {
(*this)[0][0] * op[0][0] + (*this)[0][1] * op[1][0],
(*this)[0][0] * op[0][1] + (*this)[0][1] * op[1][1],
(*this)[1][0] * op[0][0] + (*this)[1][1] * op[1][0],
(* |
|
c******t 发帖数: 27 | 40 The hiring manager discribed what his group does. It's a senior position in
Website Request Handling Group.
1. Chat a little bit.
2. Gave an example of the business and technical decisions you didn't agree
with your manager, and how did you handle it?
3. Difference between composition and inheritance in C++?
4. What's const in C++? What does "int f() const" do?
5. Difference between const char * and char * const?
6. Give large transaction data, for each transaction, you have customer id,
request |
|
s*****r 发帖数: 773 | 41 const member function
int f() const
is a function which is not supposed to modify any thing excluding mutable variable.
vs.
const int f()
a function returns const |
|
l******o 发帖数: 144 | 42 随便写了一个,懒得去测试了。
#include
#include
#include
#include
#include
using namespace std;
template
struct Segment
{
T left;
T right;
};
template
struct LeftEndComp
{
bool operator () (const Segment& one, const Segment& two) const
{
return one.left < two.left || (!(two.left < one.left) && one.right <
two.right);
}
};
template
T coverage(const vector >& segments)
{
if(se |
|
I**********s 发帖数: 441 | 43 问了1) 研究, 2) 多线程程序设计, 3) 任意无穷字符串流, 内存有限, 找出唯一一对
重复字符串, 这个我说了哈希表和外部排序, 但是面试人说有更好的办法(后来想也许
是bloom filter), 然后追问外部排序的细节到结束. 估计要挂 :(
总结: 面试既是技术活, 又是运气活.
无论如何, 把我的准备工作放下面, 攒点rp, 希望对大家有所帮助.
Interview Qs
Data Structures
1. Integer
- find number of 1s
- next largest smaller
- smallest larger number
- determine if is palindrom
- itoa, atoi
- add 2 numbers w/o using + or arithmetic operators
- implement *, -, / using only +
- find max of two numbers w/o co... 阅读全帖 |
|
c**********e 发帖数: 2007 | 44 If a class has + and = operator overloaded, and three objects a, b, c
constructed, which of the following is illegal?
a) (c=a+a)=b+c;
b) a=a=b+c;
More detail:
____________________
class X {
public:
X& operator=(const X& rhs);
const X& operator+(const X& rhs) const;
private;
int n;
};
int main() {
X a, b, c;
// Statement goes here
return 0;
} |
|
m******e 发帖数: 353 | 45 #include
#include
void countOnesAndZeros(int &numZeros, int &numOnes, const std::vector &
sequence) {
numOnes = 0;
numZeros = 0;
for(size_t i = 0; i
sequence[i] == 1 ? ++ numOnes : ++ numZeros;
}
}
int trimLeft(const std::vector &sequence, int start, int numToTrim, int
untilSeenSymbol) {
int trimCnt = 0;
while(start + trimCnt >= 0 && start + trimCnt < (int) sequence.size() &&
numToTrim > 0) {
if(sequenc... 阅读全帖 |
|
l*******y 发帖数: 1498 | 46 class A
{
private:
string name;
public:
....
friend ostream& operator<< (const ostream& out, const A& a);
};
ostream& operator<< (const ostream& out, const A& a)
{
out << a.name;
return out;
} |
|
l********y 发帖数: 1327 | 47 一种是直接返回reference,一种是返回pointer,请问各位大侠哪个写法好?还是都行
?谢谢
class singleton{
singleton(){}
singleton(const singleton&);
singleton& operator=(const singleton&);
~singleton();
public:
singleton& getInstance(){
static singleton s;
return s;
}
};
class singleton{
singleton(){}
singleton(const singleton&);
singleton& operator=(const singleton&);
~singleton();
static singleton * s;
public:
singleton * getInstance(){
s = new singleton();
return s;
}
}; |
|
g*****1 发帖数: 998 | 48 g++里warning:deprecated conversion from string constant to 'char*'
刚试了"char*" to "const char*" in vc 还是不行。。
vc里的compile error很多,以下是第一个:
g:\program files\microsoft visual studio 10.0\vc\include\algorithm(4296):
error C2784: 'bool std::operator <(const std::move_iterator<_RanIt> &,const
std::move_iterator<_RanIt2> &)' : could not deduce template argument for '
const std::move_iterator<_RanIt> &' from 'std::basic_string<_Elem,_Traits,_
Ax>'
1> with
1> [
1> _Elem=char,
... 阅读全帖 |
|
s******c 发帖数: 1920 | 49 任意取两点,计算斜率和截距,能得到一条直线,然后把其他落在这条直线上的点也打
印出来。用一个set保存unique的(斜率,截距),这样不会重复选取。特殊情况是斜
率为无穷大,没有截距。
=============================================
#include
#include
using namespace std;
#define N 5
class Line{ //y=a*x+b
public:
float a,b;
Line ():a(0),b(0){}
Line (int n1,int n2):a(n1),b(n2){}
bool operator<(const Line &right)const{
if (a==right.a)
return b
else return a
}
bool operator==(const Line &right)const{
... 阅读全帖 |
|
i**********e 发帖数: 1145 | 50 class Foo {
// bla bla bla
};
class A{
public:
Foo foo1() {
return x;
}
const Foo &foo2() {
return x;
}
private:
Foo x;
};
通常没必要的话直接返回 Foo object 就可以了。
返回 const Foo & 主要是在以下的情况:
Foo x = A().foo2();
避免不必要的两次 copy(例如:A().foo1())。但是现在的编译器都很聪明,A().foo1
() 这种情况也只 copy 一次。
还有一种情况返回 const Foo & 的目的就是避免以下奇怪的情况:
(A().foo2() = x) = y;
如果 foo2() 的返回值定义为 Foo & 就反而会编译通过。
想想看 assignment operator 的返回值你就知道我在说什么意思了。在 primitive
type 里:
int x, y, z;
(x = y) = z;
虽然以上的含义很多人都搞不懂,但是语法是对的。
简单来说,在你自己定义的 user type,如... 阅读全帖 |
|