Topic: pointer to function in class


Author: lam@saifr00.cfsat.honeywell.com (Josh Lam)
Date: 20 Nov 91 03:01:53 GMT
Raw View
Can I do the follow?

class A
{
 public:

  ....

 int function1(int *, int);
 int function2(int *, int);

 int (*func_ptr[])(int *, int) =
   {
     function1,
     function2
   }
}

I tried to compile that by the compiler tells me that
1) error: initializer for member func_ptr
2) error: dimension missing for the array A::func_ptr



Please comment.

Thanks

Josh




Author: jeburke@jhunix.hcf.jhu.edu (John Burke)
Date: 20 Nov 91 22:07:55 GMT
Raw View
In article <1991Nov20.030153.12227@saifr00.cfsat.honeywell.com> lam@saifr00.cfsat.honeywell.com (Josh Lam) writes:
>class A
>{
> public:
> int function1(int *, int);
> int function2(int *, int);
> int (*func_ptr[])(int *, int) =
>   {
>     function1,
>     function2
>   }
>}
>
>I tried to compile that by the compiler tells me that
>1) error: initializer for member func_ptr
>2) error: dimension missing for the array A::func_ptr

You can't do this.

You cannot initialize a class member inside the class's declaration.
(Non-static) Members are associated with instances of a class, and can
only be initialized with respect to an instance.  (That's error
message 1 down.)

If you want to initialize a (non-static) member, use one of the class's
constructors.

Also, you are not allowed to have non-static, un-dimensioned arrays in
a class.  See the ARM, section 9.2.  (That's error message 2.)

Good luck!

John Burke
===============
"So if you have a date in Constantinople, she'll be waiting in Istanbul!"