Hey guys,

I written a base class (AudioTrackElement), and derived a class
from there (Looper) and another (VolumePan). The looper class implements functionality,
while the AudioTrackElement is there just so I can hold many Loopers and VolumePan
objects in the same vector.

The problem: I can create a std::vector<AudioTrackElement> no problem,
but when I add a looper to this vector, it calls the base class (AudioTrackElement),
NOT the Looper instance I actually added.

The function I'm calling on the AudioTrackElement vector is process(float* L, float* R, int) . What I want it to do, is access the first element of the vector, and call process() on that instance.


Shown below is AudioTrack.hpp (where the vector is stored)

class AudioTrack
{
	private:
		std::vector<AudioTrackElement> elementVector;
};

Shown below is AudiotrackElemenet.hpp (modified for brevity)

class AudioTrackElement
{
	public:
		AudioTrackElement();
		~AudioTrackElement();
		
		void process(float *L, float *R, int nframes);
};

Shown below is Looper.hpp (modified for brevity)

class Looper : public AudioTrackElement
{
	public:
		void process(float *L, float *R, int nframes);
};

Shown below is the code in AudioTrack.cpp (That acually calls process() )

elementVector[0].process( &internalL[0], &internalR[0], nframes);

Again it compiles fine, doesn't segfault, but it calls AudioTrackElement::process() rather than Looper::process() .

Cheers for reading, hoping somebody knows whats up!
-Harry

PS: If more source is needed, shout. Its all here :-)

Recommended Answers

All 2 Replies

When you add a Looper, it is 'sliced' to it's base class.
What you need to do is store pointers or references to AudioTrackElement, they will 'preserve' the class hierarchy.

So:

AudioTrackElement* pNewEl = new Looper();
pNewEl->process( ... ); // calls Looper::process

delete pNewEl;
Looper newLoop;
AudioTrackElement newEl = newLoop; // Here it slices the object
newEl.process( ... ); // calls AudioTrackElement::process
AudioTrackElement* pNewEl = new Looper();
pNewEl->process( ... ); // calls Looper::process

delete pNewEl;
Looper newLoop;
AudioTrackElement newEl = newLoop; // Here it slices the object
newEl.process( ... ); // calls AudioTrackElement::process

Cheers that done it...
In the mean time I've realized that the Boost library has a "shared pointer" which
would be idea for what I'm trying.

Thanks, -Harry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.