• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Abstract Class C++

Fryguy8

[H]ard|Gawd
Joined
Sep 26, 2001
Messages
1,707
I've got a function for my memory management simulator, runSimulation() that is taking a MManager object.

However, since I'm supposed to be implementing various algorithms, I made a pure virtual base class, and then just instantiate MManager as the proper subclass. The problem with this is MManager is pure virtual, so I can't instantiate it.

Code:
class MManager
{
	public:
		virtual void compaction() = 0;
		virtual int grab_memory(Process*) = 0;
		virtual void free_memory(Process*) = 0;
}

and then I have a class like:

Code:
class WorstFit : MManager
{
public:
void compaction();
int grab_memory(Process*);
void free_memory(Process*);

private:
deque<Process*> mem;
}

What's the best way to do this so I can write the simulation using MManager objects in the actual simulation. Should I just convert the MManager methods to do-nothing methods instead of pure-virtual?
 
Why are you trying to instantiate MManager? You should be using MManager as an "interface" to mask the implementation (BestFit, WorstFit, etc.). It's been a while since I wrote C++, but can't you do something like:

Code:
MManager* memoryManager = new BestFit();

Using something like this would benefit from the factory pattern to hide the implementations all together. The factory would be something like:

Code:
class MManagerFactory
{
    public:
             inline MManager* getManager(void){ return new BestFit(); }
}

Of course substitute whatever implementation of MManager you want for BestFit in the above code, or make separate methods, or even better pass in an enumerated type telling the function which memory manager type to instantiate.
 
MManager* memoryManager = new BestFit();

works if MManager contains pure virtual functions? This is exactly what I was planning on doing, but when I looked up some syntax necessary for the pure virtual function my documentation suggested that this wouldn't work.

All well, must have just misread, I'll try it and see.

Thanks.
 
^ You're confusing references and objects. Abstract class types can be used to point to objects instantiated from classes that extend that Abstract base class. That's polymorphism.

And:

MManager* memManager; doesn't create an object

MManager memManager; does
 
Back
Top