Scope problem when filling a vector of base class pointers.

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
JesseC
Messages
247
Reaction score
2
I'm trying to write a simple program to calculate the impedance of an AC circuit consisting of an arbitrary number of resistors, capacitors and inductors combined in series or parallel. First I ask the user to create the components they wish to use in the circuit, and this is where I hit the problem:

Code:
	cout<< endl 
		<< "Please create the components you wish to use." << endl
		<< "Enter R for a resistor, C for a capacitor, I for" << endl
		<< "an inductor or enter 'S' to stop creating components." << endl;

	char componentType;
	cin >> componentType;

	// vector of base class pointers to store the components
	vector <component*> componentLibrary;

	switch(componentType)
	{
	case 'r':
	case 'R':
		{
		cout << "What is the resistance of the resistor in ohms?" << endl;
		double tempResistance;
		cin >> tempResistance;
                // create resistor according to user input
		resistor R(tempResistance);
		// place resistor in component library
		componentLibrary.push_back(&R);

		break;
		}
	case 'c':
	case 'C':
		{
			cout << "What is the capacitance in farads?" << endl;
		...
                ... etc
	}

	
	
	cout << "Test Output:" << endl;
	cout << componentLibrary[0]->getImpedance() << endl; // code fails here.

I know why this code fails, because the resistor object goes out of scope when the switch statement ends so the component library doesn't contain anything...

what I'm not sure about is how to get around this in a simple way?? I would hit the same problem with IF statements...
 
Physics news on Phys.org
I assume resistor is a class derived from a base class component. Instead of
Code:
resistor R(tempResistance);
you want
Code:
component* p = new resistor(tempResistance);

(Later on you'll need something to delete the components).
 
Allocate the resistor object dynamically using 'new'. Then it won't disappear on you automatically at the end of scope.

Code:
resistor *resistorPtr = new resistor(tempResistance);
componentLibrary.push_back(resistorPtr);

I think you can combine these steps and eliminate the pointer variable:

Code:
componentLibrary.push_back(new resistor(tempResistance));

You need to be very careful about memory management with this technique, though. If you ever want to "throw away" a component, you need to explicitly release the memory that it uses. For example, if you need to remove a component from the "library", without putting the component somewhere else:

Code:
delete componentLibrary[k];  // Releases the memory that componentLibrary[k] points to.
// Next, do whatever you do that removes the pointer itself from the vector.

Otherwise you have a "memory leak." The now-inaccessible memory chunk consumes some of your memory space until the program finishes, at which point the program's entire memory space reverts back to the operating system.
 
Last edited:
Thanks for the help! Working fine now.