我有以下几类:
class FixedByteStream {
public:
FixedByteStream() : size(0), address(NULL), existing(false) {}
FixedByteStream(int length) : existing(false) {
size = length;
address = new char[length];
}
FixedByteStream(int length, char* addr) : existing(true) {
size = length;
address = addr;
}
FixedByteStream(string* str, bool includeNull = false) : existing(true) {
size = (*str).length();
address = const_cast<char*>((*str).c_str());
if (includeNull){
++size;
}
}
~FixedByteStream() {
if (existing == false) {
delete [] address;
}
address = NULL;
}
int getLength() {
return size;
}
char* getAddressAt(int index) {
return &(address[index]);
}
char& operator[] (const int index) {
return address[index];
}
operator char*() {
return address;
}
private:
bool existing;
int size;
char* address;
};
能够产生问题的非常简单检验:
FixedByteStream received;
received = FixedByteStream(12);
received[0] = t ;
Valgrind warns about an invalid write, and debugging has shown why. FixedByteStream received;
calls the constructor with no arguments (which is kind of stupid because it can t do anything). received = FixedByteStream(12);
calls the constructor with the integer argument... and then immediately calls the destructor on itself, invalidating the object. It still works for some reason, but I d rather it not be placed in such an odd predicament that throws warnings.
因此,为什么有人叫它呢? 我可以理解的是,如果叫到“first,去除无用的暂时物体(而不是需要),那么,我就几乎在任何地方都使用了这种宣布为现在的签字模式,而且以前从未有过这样的问题。