Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > OOP casting classes (using one single variable for similar types of classes)

Reply
Thread Tools

OOP casting classes (using one single variable for similar types of classes)

 
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      01-06-2006

Michael wrote:

> Isn't it possible to check the audio format of the incoming audio material
> at first and then casting the IN and OUT variable to the proper type of
> audio class?


You might consider the "factory method" or "abstract factory" design
patterns.

 
Reply With Quote
 
 
 
 
Michael
Guest
Posts: n/a
 
      01-06-2006
Hi,

I think my problem deals with class casting and inheritance.

I want to deal with various Audio Formats, reading into memory for
modification, working with it (done by different classes), and writing the
result to disk afterwards.

Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
MP3FileIO and AuFileIO for the In/Out operations.
They all are inherited from the AudioFileIO class, because they all share
common methods like readSamples, writeSamples, readHeader, writeHeader.
Only the implementation is sometimes different.

Now, I want to use them in a let's say generic way:

[1]
while(there is data)
{
IN.readSamples(); // using the classes mentioned above

DelayEffect.work(); // modify the memory buffer(sample data)

OUT.writeSamples(); // using the classes mentioned above
}

But due to different audio formats, the type of the IN and OUT variable
depends on the desired audio format the user specifies.
I do not want to code every time many IF-THEN-ELSE statements for checking
the desired format and for using the proper variable.

Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?
Like:

IF the incoming file is a WAV, then IN = Type of WaveFileIO
ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO

// then using the code fragment [1], and other code fragments like [1]


Ciao, Michael


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-06-2006
Michael wrote:
> I think my problem deals with class casting and inheritance.
>
> I want to deal with various Audio Formats, reading into memory for
> modification, working with it (done by different classes), and writing the
> result to disk afterwards.
>
> Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
> MP3FileIO and AuFileIO for the In/Out operations.
> They all are inherited from the AudioFileIO class, because they all share
> common methods like readSamples, writeSamples, readHeader, writeHeader.
> Only the implementation is sometimes different.
>
> Now, I want to use them in a let's say generic way:
>
> [1]
> while(there is data)
> {
> IN.readSamples(); // using the classes mentioned above
>
> DelayEffect.work(); // modify the memory buffer(sample data)
>
> OUT.writeSamples(); // using the classes mentioned above
> }
>
> But due to different audio formats, the type of the IN and OUT variable
> depends on the desired audio format the user specifies.
> I do not want to code every time many IF-THEN-ELSE statements for checking
> the desired format and for using the proper variable.
>
> Isn't it possible to check the audio format of the incoming audio material
> at first and then casting the IN and OUT variable to the proper type of
> audio class?
> Like:
>
> IF the incoming file is a WAV, then IN = Type of WaveFileIO
> ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO
>
> // then using the code fragment [1], and other code fragments like [1]


Your problem is in the incomplete design.

Apparently, all "Audio Formats" should derive from a common base class,
which should have the 'readSamples', 'writeSamples', and other functions
and you should be instantiating proper derived class object into a pointer
to the base class:

AudioFileIO *IN = 0;
switch (in_file_type) {
case WAVE: IN = new WaveFileIO(???);
break;
case AIFF: IN = new AiffFileIO(???);
break;
...
}

// similar for 'OUT'

Then you should use your 'IN' and 'OUT' pointers _polymorphically_. Read
about polymorphism, inheritance, abstract base classes, etc.. That's what
makes C++ well-suited for OOP -- class hierarchies.

Come back when you redesign your application with polymorphism in place.

V
 
Reply With Quote
 
lucy.chian@gmail.com
Guest
Posts: n/a
 
      01-07-2006
q

 
Reply With Quote
 
lucy.chian@gmail.com
Guest
Posts: n/a
 
      01-07-2006
q

 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      01-07-2006

Victor Bazarov wrote:

> AudioFileIO *IN = 0;
> switch (in_file_type) {
> case WAVE: IN = new WaveFileIO(???);
> break;
> case AIFF: IN = new AiffFileIO(???);
> break;
> ...
> }
>


Bad idea, I think that astract factory pattern is in place......

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-07-2006

"Michael" <(E-Mail Removed)-chemnitz.de> wrote in message
news:dpmk66$prc$(E-Mail Removed)-chemnitz.de...
> Hi,
>
> I think my problem deals with class casting and inheritance.
>
> I want to deal with various Audio Formats, reading into memory for
> modification, working with it (done by different classes), and writing the
> result to disk afterwards.
>
> Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
> MP3FileIO and AuFileIO for the In/Out operations.
> They all are inherited from the AudioFileIO class, because they all share
> common methods like readSamples, writeSamples, readHeader, writeHeader.
> Only the implementation is sometimes different.
>
> Now, I want to use them in a let's say generic way:
>
> [1]
> while(there is data)
> {
> IN.readSamples(); // using the classes mentioned above
>
> DelayEffect.work(); // modify the memory buffer(sample data)
>
> OUT.writeSamples(); // using the classes mentioned above
> }
>
> But due to different audio formats, the type of the IN and OUT variable
> depends on the desired audio format the user specifies.
> I do not want to code every time many IF-THEN-ELSE statements for checking
> the desired format and for using the proper variable.


What, exactly, do you mean "the type of the IN and OUT variable depends on
the desired audio format the user specifies." Why isn't the IN and OUT
variables members of the audio formats themselves? Why aren't they just
functions? Why do they depend on the type of the desired audio format?
Where do IN and OUT get their data to work on?

It sounds like you just need polymorphism, but it could also be that you
need RTTI. I don't understand what you are trying to do in your code
snippet. You don't specify what type IN and OUT can be nor why you need
them.

I'm sure there's many ways to do what you want, but I don't understand what
it is you're trying to do.

>
> Isn't it possible to check the audio format of the incoming audio material
> at first and then casting the IN and OUT variable to the proper type of
> audio class?
> Like:
>
> IF the incoming file is a WAV, then IN = Type of WaveFileIO
> ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO


Using RTTI you can check the type of a polymorphic variable. But is this
what you are really needing to do? I'm just not sure.

In case you are, though, it would something like this:

if ( typeid( MyVar ) == typeid( MyClass ) )

so, for instance, if your polymorphic variable was stored in the variable
format it would be:

if ( typeid( Format ) == typeid( WaveFileIO ) )

although it might be a differernt if Format is a pointer. I *think* this
syntax is what you need (not positive)

if ( typeid( Format ) == typeid( WaveFileIO* ) )

>
> // then using the code fragment [1], and other code fragments like [1]
>
>
> Ciao, Michael



 
Reply With Quote
 
Bob Hairgrove
Guest
Posts: n/a
 
      01-07-2006
On Fri, 6 Jan 2006 21:38:10 +0100, "Michael" <(E-Mail Removed)-chemnitz.de>
wrote:

>Hi,
>
>I think my problem deals with class casting and inheritance.


Virtual functions sounds like the way to go.

>I want to deal with various Audio Formats, reading into memory for
>modification, working with it (done by different classes), and writing the
>result to disk afterwards.
>
>Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
>MP3FileIO and AuFileIO for the In/Out operations.
>They all are inherited from the AudioFileIO class, because they all share
>common methods like readSamples, writeSamples, readHeader, writeHeader.
>Only the implementation is sometimes different.
>
>Now, I want to use them in a let's say generic way:
>
>[1]
>while(there is data)
>{
> IN.readSamples(); // using the classes mentioned above
>
> DelayEffect.work(); // modify the memory buffer(sample data)
>
> OUT.writeSamples(); // using the classes mentioned above
>}
>
>But due to different audio formats, the type of the IN and OUT variable
>depends on the desired audio format the user specifies.
>I do not want to code every time many IF-THEN-ELSE statements for checking
>the desired format and for using the proper variable.
>
>Isn't it possible to check the audio format of the incoming audio material
>at first and then casting the IN and OUT variable to the proper type of
>audio class?
>Like:
>
>IF the incoming file is a WAV, then IN = Type of WaveFileIO
>ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO
>
>// then using the code fragment [1], and other code fragments like [1]
>
>
>Ciao, Michael
>


In addition to the Abstract Factory design pattern, the Strategy
pattern might serve you well for this.

--
Bob Hairgrove
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-07-2006
puzzlecracker wrote:
> Victor Bazarov wrote:
>
>> AudioFileIO *IN = 0;
>> switch (in_file_type) {
>> case WAVE: IN = new WaveFileIO(???);
>> break;
>> case AIFF: IN = new AiffFileIO(???);
>> break;
>> ...
>> }
>>

>
> Bad idea, I think that astract factory pattern is in place......


And inside that "abstract factory", what are you going to do? And if
that factory is abstract, how do you instantiate it? Through another
abstract factory? Think before you post your "bad idea" replies.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
Any similar Webcam broadcasting site similar to youtube Chaudhry Nijjhar Computer Support 0 02-19-2008 11:48 PM
Casting from pointer types to non-pointer types jois.de.vivre@gmail.com C++ 11 06-07-2007 09:17 AM
types of classes in template classes lobequadrat@googlemail.com C++ 5 04-27-2007 05:33 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM



Advertisments