Re: [genuine] Re: reading Midi from AU
Re: [genuine] Re: reading Midi from AU
- Subject: Re: [genuine] Re: reading Midi from AU
- From: jacques couzteau <email@hidden>
- Date: Mon, 12 Jan 2004 16:51:23 +0100
hello philippe,
thank you for your detailed explanations. They are indeed very very
helpful to me. I believe i now fully understand the semantics of static
members. in my own words i would call them class globals.
Since a there have been a lot of comments about a lack of tutorials and
documentation i would like to point out that the support i get from you
and this mailing list ist truly great and i appreciate that highly.
Respect!
I know have a working version, which uses
a static member function
static void SampleEffectUnit::SampleEffectKernel::MyReadProc(const
MIDIPacketList *pktlist, void *refCon, void *connRefCon)
which reads
classic-C-global Variables (defined outside of any class)
MIDIPortRef gOutPort = NULL;
MIDIEndpointRef gDest = NULL;
int gChannel = 0;
As you say i should be able to define those as static members of class
SampleEffectUnit or SampleEffectKernel. I don't doubt that at all, but
i have a problem compiling it. I get a syntax error (syntax error
before `::' token) where i allocate and initialize my static vars:
Here is my Code:
// Allocation and initialisation of the static data members
MIDIPortRef SampleEffectUnit::SampleEffectKernel::gOutPort = NULL;
//syntax error before `::' token
MIDIEndpointRef SampleEffectUnit::SampleEffectKernel::gDest = NULL;
//syntax error before `::' token
int SampleEffectUnit::SampleEffectKernel::gChannel = 0; // syntax
error before `::' token
class SampleEffectUnit : public AUEffectBase
{
public:
...
protected:
class SampleEffectKernel : public AUKernelBase // most real work
happens here
{
public:
...
private: //state variables...
static MIDIPortRef gOutPort;
static MIDIEndpointRef gDest;
static int gChannel;
...
static void MyReadProc(const MIDIPacketList *pktlist, void *refCon,
void *connRefCon);
...
}
}
/Developer/Examples/CoreAudio/AudioUnits/Frequenzstrom/
SampleEffectUnit.cpp:65: error: syntax error before `::' token
I just don't understand why i get a syntax error here.
>
> Why can a static memberfunction not access private or public member
>
> variables?
>
>
A static method or data member is shared by all the instances of your
>
class. You don't need to have a pointer or a reference to any instance
>
to access static members. There's just like "classic" C global
>
functions or variables, except that they are defined in a name scope
>
which is the name of your class.
>
To call a non static method or to access a non static data member you
>
need a pointer or a reference to a particular instance of the class.
>
A small example will make it clear.
>
>
class A {
>
public:
>
void method1();
>
int member1;
>
>
static void method2();
>
static int member2;
>
};
>
>
To access method1 or member1 you write something like:
>
>
A a = new A;
>
....
>
a->method1();
>
int i = a->member1;
>
....
>
>
You need the pointer "a" to the instance to do this.
>
>
To access static method2 or member2 you write something like:
>
>
A::method2();
>
int i = A::member2;
>
>
In that case you don't need a particular instance. Note that writing:
>
>
a->method2();
>
int i = a->member2;
>
>
is also legal in modern C++.
>
>
Don't forget that there is always a hidden parameter which is passed
>
to any non static method. It is called "this" as you probably know. It
>
is the pointer on the instance on which you make the call. This is why
>
you can use a non static member within a non static method body either
>
by typing this->member1 or simply member1.
>
>
>
>
> I thought static just causes the compiler to allocate memory on the
>
> heap instead of the stack for the function an limits it's scope to the
>
> current module. Does it need to be in the same block of memory in
>
> order
>
> to access members?
>
>
The meaning of static has evolved when C++ was invented. The
>
definition you give is still valid in C++ but applies to variables
>
declared out of any scope (this is the C meaning of static).
>
>
The memory needed for a static class data member **must** be
>
explicitly allocated. For instance in the simple example above, you
>
**need** to write:
>
>
int A::member2;
>
>
somewhere in a .cpp file otherwise you will get an unresolved
>
reference at link time.
>
>
>
>
> Does the function need to be static in order to be passed as a
>
> function-pointer to another function?
>
>
Yes, because of the hidden "this" parameter for non static methods.
_______________________________________________
coreaudio-api mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/coreaudio-api
Do not post admin requests to the list. They will be ignored.