Reputation: 10539
This is probably a duplicate, but suppose we have following code
#include <stdio.h>
#include <stdlib.h>
class IPair{
public:
IPair(const char *s) : s(s){
buffer = malloc(1024);
};
virtual ~IPair(){
free(buffer);
}
virtual void print() const = 0;
const char *s;
void *buffer;
};
class Pair : virtual public IPair{
public:
Pair(const char *s) : IPair(s){};
virtual void print() const override{
printf("Hello %s\n", s);
}
};
Pair createPair(const char *s){
return Pair(s);
}
IPair & usePair(IPair &pair){
pair.print();
return pair;
}
int main(int argc, char** argv)
{
Pair a = createPair("AAA");
Pair b = createPair("BBB");
usePair(a);
IPair &x = usePair(b);
usePair(x);
return 0;
}
If examining with valgrind, there is no memory leak - e.g. destructor is called on correct "place".
This raise following questions:
createPair()
? Looks like object memory is copied. Is it memcpy()-like or have something to do with copy constructor? Upvotes: 0
Views: 81
Reputation: 5230
P.S. Declare destructor of IPair virtual...
Upvotes: 3
Reputation: 129344
Like Copy Constructor - if no constructor is provided, the compiler will make one, which is essentially memcpy(*this, rhs, sizeof(*this));
. Return value optimisation, which is that the compiler passes in the address of the destination to store the value in - removing the need for a copy in most cases. [Actually, all structure/class returnes will modify the call such that the compiler passes the object to be returned as a reference parameter]
References are nearly the same as pointers in C++, so when you return a reference, the address of the object is returned. The difference is that references don't need a dereference operator, and can only ever be assigned once.
Upvotes: 3