hello, i have this class called MemoryManager,
it is supposed to implement a simple smart pointer, (count reference);
i have a vector where i store the requested pointers,and i return the index of the pointer to the caller..
when a user creates a pointer of type MemoryManager he calls an initializer function called modified_malloc(size_t) , create a MemoryManager obj, alloc a memory space and store it into data,increase count, and store the object into global_MM_vecotr , and return the index as a pointer , when the use tries to use indirection ( ->) i return the appropriate real pointer from the vector, according to the index value..
class MemoryManager
{
public:
//operators overloading prototypes
private:
void* data;
int count ;
};
std::vector<MemoryManager*> global_MM_vecotr;
void* MemoryManager::operator=( void* x)
{
// some code here
}
the problem i am facing is that i overloaded a couple of operators, however when i try to run the code below the "=" operator doesn't get called.. can some1 point the problem out to me..
//the main code
{
MemoryManager* obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a MemoryManager obj in a vector;
MemoryManager* obj2 = obj1 ;
}
Edit: already tried the following , no change
{
MemoryManager*obj1 = (MemoryManager*) x->fun1(4); //fun1 returns an index to a Class obj in a vector;
MemoryManager*obj2 ;
*obj2 = *obj1;
}
{
MemoryManager* obj1 = ( MemoryManager*) x-> fun1(4);
MemoryManager* obj2;
obj2.operator =(*obj1);
}
-
See spec, you cannot override pointer basic operations.
Konrad Rudolph : You should beef this answer up a bit, it's the correct one, but hard to understand.Dewfy : @Konrad - I would like to cite Stroustrup, but unfortunately have only Russian version. -
Might be a technicality, but you're not assigning a
ClassA
, you're assigning aClassA*
(ie, a pointer). I might be way off here, but this is where I'd lay the blame. -
From you code, you have defined
operator=
for theMemoryManager
class taking avoid*
.Your example code is initializing
ClassA
pointers and not assigning toMemoryManager
instances.There are three reasons why your code is not being called.
- You are initializing not assigning, so if anything a constructor would be called rather than an assignment operator.
- You are initializing pointers and not objects, pointers are basic types and you cannot provide overloaded operators for them.
- You are using
ClassA
and notMemoryManager
which you have actually provided theoperator=
for.
Madi D. : i am sry about the misunderstading.. classA is the same as memorymanager.. i was in the process of renaming my "gibberish" names into more meanigful names.. when i posted the qus..Charles Bailey : Fair enough, then you only need to address the other two points.Madi D. : i edited the codes .. and omitted the use of ClassA :) , i moved the equality to after i created the object. can u check the edited description and tell me what can i do to avoid using pointers :SCharles Bailey : You are using lots of pointers, if you don't want to use them, stop using them! Perhaps you should post the full definition of `MemoryManager` and an explanation of what it's trying to achieve? With the small snippets with a slew of pointers and casts it's not easy to see what you are trying to do.Madi D. : my friend, thx alot for ur clearification, after much thought..i am re-constructing the whole project using templates instead of class/void* .. again thx alot for ur help -
I suspect you're using the void pointer so that you can enter any kind of object. I'd recommend using a template instead combined with the boost::check library.
Madi D. : oddfellow: :) thx alot.. already made the decision
0 comments:
Post a Comment