Pointers as Arrays

I'm trying to pass a "container" of objects into a method and from what I've been told, doing something like: container[int] works for calling the i'th object in that container. Am I wrong in that sense?
It's not clear at all what you mean. Please post a simple example.

is that container[int] a prototype? an argument? a parameter? or the hell that I've been seeing in this forum with people post 10 words questions without really bothering whether the people who read are ever gonna understand what they wrote!!!! Stop throwing questions blindly!!!!
Here's what the line looks like:

 
waves[ pos ].Play(waves);


waves is a dynamically allocated array, with wav files in it, not files but the parts used to create a wav. And then pos is an unsigned int that has the position in the waves array that has the wav file information I want to grab. That is what I want to pass into Play();.
What is play? and where is it defined? and how could it be a member of a dynamically allocated array? what does it mean that the array includes "files". Is it file names? or binary data of the audio you wanna play?
Play() method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Wav::Play() // Play() writes the wav file, needs to be called first
{
	// setup a WAV_OUT object to write the file to disk
	WAV_OUT outfile( sampleRate, bitsPerSample, channels );

	// add the samples to the outfile object
	for( unsigned int i=0; i<sampleCount; i++ )
	{
		outfile.write_current_output( samples[i] );
    }

	// and write it out to a temporary file
	outfile.save_wave_file( "out.wav" );
 
	// call the windows API to play that temporary file
	PlaySound( TEXT( "out.wav" ), NULL, SND_FILENAME );
}


Container class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class WavContainer : public Wav
{
private:
	Wav *Wavs;
	int size, position;
public:
	// CONSTRUCTORS
	WavContainer();
	WavContainer(int);
	WavContainer& operator=( const WavContainer& other );
	WavContainer( const WavContainer& to_copy );

	// DESTRUCTOR
	~WavContainer();

	// METHODS
	void Add(Wav &); // adds a new wav object to the wavcontainer
	void Expander();
	int Size();
	void Show();
	void Remove(int pos);
	int GetPos() { return position; };

	// METHODS FOR CALLING THE CORRECT METHODS ON THE WAV'S AND GETTING USER INPUT
	void input(string&, int&, int&, string&);
	void selection(string&, int&, int&, string&, Wav&);
};


selection(string, int, int, string, Wav); method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
void WavContainer::selection(string &method, int &pos, int &posTwo, string &cleanWave, Wav &waves)
{
	// Use the prompt object to call the methods	
	if(method == "play")
	{
		//waves[ pos ].Play(waves); // This is what I think should work but it doesn't.
		waves.Play(waves);
	}
	else if(method == "delete")
	{
		//CALL THE REMOVE METHOD;
		Remove( pos );
	}
	else if(method == "reverse")
	{
		//CALL THE REVERSE METHOD
	}
	else if(method == "half")
	{
		//CALL THE HALF METHOD
	}
	else if(method == "double")
	{
		//CALL THE DOUBLE METHOD
	}
	else if(method == "mix")
	{
		//CALL THE MIX METHOD
	}
@TheDestroyer: It seems like you're being intentionally dense. (And quite rude, to be honest.)

@incremental:
If your container is a dynamically allocated array, that means you can pass the pointer to a function to pass the array, e.g.:
1
2
3
4
int* myInts = new int[10];
void doubleNumbers(int* array, unsigned size) {
    for (unsigned i = 0; i < size; ++i) array[i] *= 2;
}

If your container contains Objects of a class that has some member functions, you can call those functions directly on one of the Objects, like you did. For example:
1
2
myObject* objects = new myObject[10];
objects[5].someMemberFunction();

Thus, if your "wave" object has a Play member, then you can indeed do what you propose. I'm not entirely sure why you still passed "waves" as an argument. By calling the member function on the pos'th item, all information on that item is known, thus the function can change the object if needed.
Last edited on
Your WavContainer is inherited from Wav, which contains the method play(), which means you can from an instance of WavContainer just call play and have the file passed by just including it in either of your classes. I don't see the problem here.

I don't understand your code because operator[] is not defined in your code, and I don't know from your previous post what waves[pos] should mean.
Last edited on
The operator[] is not defined error is just that I don't have the inherited object in the container class, right? That's just a simple task of putting it in the header file of my container class?
@TheDestroyer: he's using the basic dynamic array inside the class. He's not doing WavContainer[pos], he's doing Wavs[pos] inside WavContainer member functions. He doesn't need to define the accessor operator, because he's simply using an array. His idea is wrong, but I think you're simply misunderstanding his intentions.

@incremental:
You have two problems.
A) Your function 'selection' takes a Wav object, not an array of Wav objects. waves[pos] does nothing because you're calling the [] operator on a single Wav object, which is meaningless unless you define it. Change the parameter to Wav* (a pointer) and pass the entire array!

B) Play() doesn't take any parameters. Why are you trying to pass "waves" to it? It makes no sense.
@Gaminic: I'm getting driven crazy from how careless questions are thrown in this forum. Seriously, there should be some admin who deletes question that are just crap!! look at the forum and count how many questions are NEVER-answerable with how much ever one thinks.

@incremental: What kind of inclusion are you talking about? #include or some other kind? if you mean the first one, then of course you have to have it in your class file included, if the second, this would mean you don't really understand inheritance. Please read more about inheritance and what advantages you get when you inherit.

http://www.cplusplus.com/doc/tutorial/inheritance/

In a nutshell, when you inherit a class into another, the subclass will have all the methods and variables in it, and you'll have all protected and public methods and variables from the parent class accessible in your subclass.
Last edited on
@TheDestroyer: Then do everyone a favour and don't answer if a question doesn't meet your standards. I know people are often sloppy, but this question wasn't nearly as bad as you make it seem. Now, you're just getting more annoyed and incremental isn't getting the help he needs.
@Gaminic: Please do me a favour and don't interfere when you don't understand the problem of why I'm going crazy. The issue with this question is over and we're discussing. You don't need to lecture me about manners. Thank you.
Topic archived. No new replies allowed.