z*****a 发帖数: 3809 | 1 re
This is the standard for operating systems courses everywhere. |
|
g*******s 发帖数: 59 | 2 friend ostream& operator<<(ostream &, const Counter &);
为什么要加个const before class name? |
|
X****r 发帖数: 3557 | 3 先忽略你重载的operator new,把基本概念搞清楚。
从语法上来讲,void*不能自动转换成Foo*,这个是乌龟的屁股,所以你编译通不过,
从语义上来讲,rbv只是返回一片内存,new不仅分配内存还调用构造函数,这个赋值就
不对。
如果你想在指定的内存位置构造一个对象,用placement new:
Foo *y = new (rbv()) Foo;
如果你想强行把这片空间作为一个Foo对象来看待,用cast:
Foo *y = (Foo *) rbv();
但是后者这样的话你使用y的时候什么都可能发生。 |
|
X****r 发帖数: 3557 | 4 你得到它了。你重载的operator new和你调用的new expression不是一回事。 |
|
r*********r 发帖数: 3195 | 5 1. the system has this operator defined already.
2. f << 55 << 10; |
|
z****e 发帖数: 2024 | 6 const X& operator+(const X& rhs) const;
看到一个声明如上,
const的mem fun 是不能改mem data吧?
那么这种设计干什么呢?进来的是const,里边的自己人也是const,加法,加谁呀? |
|
p***o 发帖数: 1252 | 7 The correct one should be:
X operator+(const X& rhs) const;
e.g. a=b+c is a=b.opeartor+(c), so neither b nor c will be changed. |
|
g**********1 发帖数: 1113 | 8 When I try to overload operator delete in C++, I need to delete some void *.
Thus I will get warning when I compile the source code. I wonder if there
is any way to avoid the warning.
Thank you. |
|
g**********1 发帖数: 1113 | 9 When I read the book: thinking in C++. The chapter about overload new and
delete operators. I practice the code and found the warning. Also this book
metions that not deleting void *. Thus I want to know how to avoid this
problem.
Thank you. |
|
X****r 发帖数: 3557 | 10 因为new 和new []是两个不同的operator,只是共用了同一个keyword而已。
比如说你重载new ,不影响现有的new [] |
|
y***a 发帖数: 840 | 11 I tried to add a constructor in the template class NARROW: NARROW(WIDE&w) {
...}
then it works. I don't understand this, seems C++ tries to use the construct
or NARROW(WIDE& w), instaed of the type conversion operator in WIDE. Weird.
Any thoughts? |
|
t****t 发帖数: 6806 | 12 no you don't have to define narrow(wide&) (by the way, that's not the correc
t way of user-defined conversion functions. narrow(const wide&) is the corre
ct one), if you provided wide::operator narrow() (again, better use wide::op
erator narrow() const).you have some other errors in the program.
{
construct
. |
|
j****i 发帖数: 305 | 13 I want to overload operator new and delete to detect memory leaks,
according to this article: http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml
He could do it by putting the new def of new stdafx.h, but I'm programming
in unix environment,
how can achieve the same thing without changing all my source codes? |
|
j****i 发帖数: 305 | 14 Great idea!
But when I do that, boost::shared_count.hpp gives me problem when I compile.
Here's the line in boost that doesn't compile:
new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
the error message is:
/usr/local/include/boost/smart_ptr/detail/shared_count.hpp:160: error:
expected type-specifier before 'static_cast'
Seems that the placement new does not like the overloaded new.
With the following overloaded new:
void * operator new(size_t size,
|
|
G****A 发帖数: 4160 | 15 Thanks for reply, but is there any specific reason?
The intuition of my question is that an operator, say "<<", should be
associated with the class (independent of one particular object), which
matches well the definition of static member function. |
|
X****r 发帖数: 3557 | 16 Because you can simply define a function to overload the operator
in global scope (and put it in the same source file with your class
if you want). |
|
G****A 发帖数: 4160 | 17 then how about the situation where two classes nest at one source file
and each class requires its own operator overloading?
|
|
X****r 发帖数: 3557 | 18 What's the problem then? Each class overloads the same operator
in the global space. These overloaded functions differ by their
parameter list. This is what 'overloading' supposes to do. |
|
G****A 发帖数: 4160 | 19 这个问题是朋友问我的,所以我也没有source.
但让我confuse的部分是:
如果有一个member function: UPInt& operator++();
那么对于 UPInt A; A++; ++A; which one is a valid call to the member function?
谢谢大牛耐心解释. |
|
L**Q 发帖数: 834 | 20 a(b) V.S. a = b
In gerneral, if a class needs a copy constructor, it will also need an
assignment operator. |
|
X****r 发帖数: 3557 | 21 Because in C++, the (un-overloaded) assignment operator returns a
the value of its left operand, which is a l-value, see C++98 5.17
[expr.ass]. |
|
S*******s 发帖数: 13043 | 22 why not google "copy assignment operator" b4 posting? |
|
t****t 发帖数: 6806 | 23 yes i understand your question. xentar answered it, let me rephrase: for
built-in types, (a=b)=c is legitimate (from syntax point of view. it is
undefined though), so overloaded operators mimic it. why (a=b)=c is
legitimate from the beginning? because it is legitimate in C.
OP? |
|
a***y 发帖数: 2803 | 24 那你是说这个是对的?
const A & operator= (const A& other); |
|
a***y 发帖数: 2803 | 25 #include
using namespace std;
int main() {
int a[5];
a[0]=9;
a[1]=8;
a[2]=7;
a[3]=6;
a[4]='\0';
int i=1;
a[i++]=i;
a[i]=i++;
(a[3]=4)=5;
cout << "a[0] is "<< a[0] << endl;
cout << "a[1] is "<< a[1] << endl;
cout << "a[2] is "<< a[2] << endl;
cout << "a[3] is "<< a[3] << endl;
return 0;
}
结果是
a[0] is 9
a[1] is 1
a[2] is 2
a[3] is 5
(a[3]=4)=5;最后a[3]等于5,不是4. 语法正确,结果正确.
say
operators:
precedenc
kick
ta |
|
a***y 发帖数: 2803 | 26 假设a,b,c都是int.
(a=b)=c 就是
1.括号里的运算优先,a=b,然后这个int& operator=(int& ) { }的返回值是int&,因为
是一个reference,其实返回的是变量a,而不是变量a的值,这个区别很重要.
2.返回的reference int& a = c,这样就是变量a的值变为了c的值.
所以,最后a变为c的值. |
|
a***y 发帖数: 2803 | 27 string s = "hi" + "ji";
是不是left operand,right operand都是const char*,在5个函数里都没有这个
signature?
"At least one of the arguments used has to be a string object."
参考:
http://www.cplusplus.com/reference/string/operator+/
Parameters
lhs
A string object, a c-string or a character. Its content forms the
beginning of the returned object.
rhs
If lhs is a string object, this is another string object, a c-string or
a character.
If lhs is not a string object, this is a string object.
In either cas... 阅读全帖 |
|
M*********t 发帖数: 257 | 28 Since both argument expression are of const char* type
why the compiler doesn't do an automatic type conversion to convert
one of the argument to type const string&, Isn't there a
constructor in string class that enable this type conversion?
Two possible reasons
(1) Is this due to the reference symbol &, that prevents this conversion (
can't convert to a reference type?)
(2) Due to ambiguity ie. compiler doesn't know whether to convert 1st or 2nd
argument to match an overloaded operator function... 阅读全帖 |
|
t****t 发帖数: 6806 | 29 why is it so difficult to understand? you MUST have at least one user-
defined operand, such that the compiler will start to consider using
overloaded operators.
is my english that bad?
2nd |
|
t****t 发帖数: 6806 | 30 first of all, it is not up to you or me to say compiler "should" do what and
what. a rule is there and it applies, that's it.
second, why it is designed this way? suppose in addition to std::string, you
define another class A that can converts const char* implicitly, and also
defines operator+(A, A). now you write "abc"+"def". what does it mean? it is
not clear. it can mean string("abc")+string("def"), or means A("abc")+A("
def"), or string("abc")+A("def"), etc. basically a mess. compiler must
g... 阅读全帖 |
|
C***y 发帖数: 2546 | 31 如果一个atomic operation的参数是多重指针得到的,会有影响吗?
For example, on Windows
如果我调用 InterlockedAdd( p1->p2->p3->data, 5 ), 这些指针的值随时会变化,
会对最后结果产生影响吗?
谢谢! |
|
f*******n 发帖数: 12623 | 32 You are forgetting operator precedence.
&& has higher precedence than || |
|
r**u 发帖数: 1567 | 33 Given a class, why the overloaded assignment operator returns a reference to
this class? Thanks. |
|
d****i 发帖数: 4809 | 34 because you want to use '=' operator as l-value
to |
|
w*s 发帖数: 7227 | 35 hi, i'm new to this, many thanks for help !
trying to send a query packet out,
size 1454 bytes, 1st word 0xfffffffb, 2nd 0xffffffff, 3rd 0x392
the correct packet sent from c code is captured in wireshark in the picture,
but got this,
socket.error: [Errno 10051] A socket operation was attempted to an
unreachable network
The code is like this,
def send_pnp_query():
print "... send query ..."
msg = bytearray(1454)
#ptr = PNP(msg)
msg[0] = 0xfb
msg[1] = 0xff
msg[2] = 0xff
... 阅读全帖 |
|
e****d 发帖数: 333 | 36 vector > a;
a.push_back(shared_ptr |
|
b*****e 发帖数: 474 | 37 The -> operator needs a member name, so ->[] does not make sense. |
|
e****d 发帖数: 333 | 38 operator[] is eye candy and the same as a member function, they why cannot
be called through a pointer? we often call using p->f(), f is a member
function. but why p->[] does not work here? |
|
c*******y 发帖数: 1630 | 39 related with parser.
check
a[0]->operator[](x) = y; |
|
p***o 发帖数: 1252 | 40 Array::operator[] will incur no performance penalty in comparison to
[] for built-in (C) arrays after 'require' is removed.
Since |
|
N******K 发帖数: 10202 | 41 我看过机器码 operator[] 这个函数优化之后 和 直接写 Array[index] 一样
++ |
|
|
c******o 发帖数: 1277 | 43 operator 是语言spec的,没法改
function是code里的,可以override/wrap/添加 |
|
x***4 发帖数: 1815 | 44 抱歉一下: 我的意思是指那些像operator的method。比如说setter是 var_=(xyz)。 |
|
d********u 发帖数: 5383 | 45 operator overloading为什么要用copy constructor,我目测这里98%的人都不懂。
当然,他们可以说他们跟A3大妈是一个级别的。印度千人大系,澳洲tester,偶也! |
|
p***o 发帖数: 1252 | 46 你在operator=里写*this=other? |
|
m*********a 发帖数: 3299 | 47 在MyClass & operator=(const MyClass & other)中
swap(other);//copy other
return *this;
在MyClass 做swap other
class MyClass{
void swap(MyClass &other){
std::swap(this->value,other.value);
}
} |
|
l*y 发帖数: 21010 | 48 It is better to have 100 functions operate on one data structure
than 10 functions on 10 data structures.
这句话深得我心
FP征服了我
我就讨厌屁大点事建一个类,其实都是map+list而已,
比如说数据源是json,好好的map,非要转成对象套对象,非要unmarshal,其实直接再
map上操作不就完了?
弄一大堆乱七八糟毫无意义的小类和特定类上的方法,这是导致方法不能重用的罪魁祸首 |
|
f*********e 发帖数: 851 | 49 一台笔记本坏了两年没有去修,坏之前装的是xp,中间开过几次机,都是开了一会就自动重启。前几天开机出现了samsung的开机画面以后就显示operating system not found。把系统恢复盘放进光驱里面再开机还是一样。是不是没得救了? |
|
r******f 发帖数: 413 | 50 run a wrong command "rm wang* subdir"
I mean mv but it's now removed......
fortunately, I have hard copy of these
files.
BUT:
After that operation, the compile
command "pgf77 -o x.exe filename.f"
does not work any longer...... So I am
afraid some other files have been
removed.
Wuwuwu........ I don't know how to fix
the 2nd problem. And now I need run
my programs urgently. DAXIA Please
Give me a hand.
Thanks a lot
|
|