Hello All
I have a memory container that I use to help me manage memory. It helps me share memory between different module of my program and deletes the memory when the memory is not needed.
I also have an template abstract base class that I use to generalize data access of various classes. Originally, it was for primary types like long, short and such. Hence, the set function accepted arguments by value.
Recently I wrote a class that contains memory containers. I want to use the same generalized data access for these containers. Hence I am passing this memory container class by value. The real size of this class is just a little larger than a pointer, so I figured it would be a minor performance hit.
Problem is when I pass a class by value, a new instance of that class is created. Except, no constructor function is called. Yet, when the function returns, the deconstructor is called and the memory is deleted. Since no function is called on creation, I cannot check/set any static flags to determine whether the memory container is a copy (hence avoid deleting the object)
I know an easy solution would be to simply pass the class by reference. However, I would have to edit the source code for all classes that already inherit the generalized data access interface. Also, classes that use this interface would have to pass memory locations. Something like `set(2.1)' would not compile.
I have considered making a separate set_by_reference class. Yet, with that implementation my data access is no longer generalized and I will have to write two versions of all the functions that rely on that generalized interface. Again the solution is a lot of tedious code writing.
I want to avoid breaking my code so I am trying to figure out some more elegant solutions to my problem. I would be happy to hear your thoughts.
Can some wise code guru tell me whether this is compliant with the C++ standard, or is this a bug with g++? Personally, I think it makes sense that there should be a constructor call, for every deconstructor call (and vice versa). Also, I assumed a pass by value call would blindly copy the data on and off the stack. Can someone give some thoughts on how I can control the calling of either the constructor or the deconstructor in a function call? Thanks a lot for the help.
Take Care
PilotMM
Let me give you some sudo code to help describe my problem:
Code:
class memoryContainer
{
private:
pointer_to_memory memory;
info_about_memory info;
public:
memoryContainer()
{
// Set info for empty memory
...
}
~memoryContainer()
{
// If memory is set and not needed then delete it
...
}
// Other functions for setting and getting the memory and info
...
};
template<typename T>
class abstract_base_class
{
public:
// constructor and deconstructor
...
// Interface for data access
virtual void set(memoryContainer mem)=0;
...
};
class newDataContainingObject:public abstract_base_class<memoryContainer>
{
public:
void set(memoryContainer mem)
{
// set some other memory container to hold the same memory
}
};
int main()
{
memoryContainer mem;
newDataContainingObject dataObj;
// Build up one memory container with memory
...
// Set the other memory container with the set function
dataObj.set(mem);
// When this is called a new memoryContainer is created
// without the constructor being called
// The contents of the argument are copied into this new container
// When the function returns, the deconstructor of the memory
// container is called. That is when the internal memory is deleted.
}