S
stf
The question is whether function calls that use temporary objects,
like this:
testMethod(BufferWrapper("test string").getContents());
are legal. Is BufferWrapper destructor *guaranteed* to be called
*after* testMethod has returned?
Here the full code:
#include <iostream>
#include <cstring>
using namespace std;
class BufferWrapper {
public:
BufferWrapper(const char *s);
virtual ~BufferWrapper();
virtual const char* getContents() const;
private:
char *pContents;
};
BufferWrapper::BufferWrapper(const char *s) {
cerr << "BufferWrapper::BufferWrapper : " << s << endl;
int len = strlen(s);
pContents = new char[1 + len];
strncpy(pContents, s, len);
pContents[len] = 0;
}
BufferWrapper::~BufferWrapper() {
cerr << "BufferWrapper::~BufferWrapper: " << pContents << endl;
if (pContents) {
delete [] pContents;
pContents = NULL;
}
}
const char* BufferWrapper::getContents() const {
return pContents;
}
void testMethod(const char *s) {
cerr << "testMethod: " << s << endl;
}
int main() {
testMethod(BufferWrapper("test string").getContents());
return 0;
}
I also noticed that if temporary object creation is wrapped inside
inline function, then it gets destroyed before that function returns:
inline const char* f() {
return BufferWrapper("test string").getContents()
}
testMethod(f()); // On testMethod call BufferWrapper does not exist!
So, if BufferWrapper class has a very_long_name and is used very
frequently in the pattern like this (for temporary method
parameters) what can I do to make the code shorter? Use a macro??
Thank you!
STF
like this:
testMethod(BufferWrapper("test string").getContents());
are legal. Is BufferWrapper destructor *guaranteed* to be called
*after* testMethod has returned?
Here the full code:
#include <iostream>
#include <cstring>
using namespace std;
class BufferWrapper {
public:
BufferWrapper(const char *s);
virtual ~BufferWrapper();
virtual const char* getContents() const;
private:
char *pContents;
};
BufferWrapper::BufferWrapper(const char *s) {
cerr << "BufferWrapper::BufferWrapper : " << s << endl;
int len = strlen(s);
pContents = new char[1 + len];
strncpy(pContents, s, len);
pContents[len] = 0;
}
BufferWrapper::~BufferWrapper() {
cerr << "BufferWrapper::~BufferWrapper: " << pContents << endl;
if (pContents) {
delete [] pContents;
pContents = NULL;
}
}
const char* BufferWrapper::getContents() const {
return pContents;
}
void testMethod(const char *s) {
cerr << "testMethod: " << s << endl;
}
int main() {
testMethod(BufferWrapper("test string").getContents());
return 0;
}
I also noticed that if temporary object creation is wrapped inside
inline function, then it gets destroyed before that function returns:
inline const char* f() {
return BufferWrapper("test string").getContents()
}
testMethod(f()); // On testMethod call BufferWrapper does not exist!
So, if BufferWrapper class has a very_long_name and is used very
frequently in the pattern like this (for temporary method
parameters) what can I do to make the code shorter? Use a macro??
Thank you!
STF