Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Use Function Pointer Instead Friend?

Reply
Thread Tools

Use Function Pointer Instead Friend?

 
 
Immortal Nephi
Guest
Posts: n/a
 
      07-05-2008
I design a class for general purpose. I do not allow a client to read
or modify interface and implemention. I allow them to write a new non-
member function outside of class' interface and implmention. The
problem is that non-member function cannot access private data member.

The friend keyword is not the solution. I am aware that friend
keyword allows the non-member function to access private data member.

If you define more than two non-member functions, you will want one of
some non-member function to be pointed to the member function pointer
at run-time. The client can decide to remove first non-member
function from the member function pointer and add another non-member
function at run-time. The member function pointer array is not
necessary according to my design.

Does it mean that it is the only solution to access public data member
only?

Take a look at my example code below.

#ifndef A_H
#define A_H

class A
{
public:
typedef void (*Func_Ptr)(A &);

A(void);
~A(void);

void Run(void);
void Set_Func_Ptr(Func_Ptr);

private:
friend void Non_Member_Func(A &);

Func_Ptr Member_Func_Ptr;

public:
int W;
int X;

private:
int Y;
int Z;
};

#endif // A_H

// A_CPP
#include "A.h"

A::A(void)
{
W = 1;
X = 2;
Y = 3;
Z = 4;
}

A::~A(void)
{
}

void A::Run(void)
{
Non_Member_Func( *this );
Member_Func_Ptr( *this );
}

void A::Set_Func_Ptr(Func_Ptr Ptr)
{
Member_Func_Ptr = Ptr;
}

// MAIN_CPP
#include <stdio.h>
#include "A.h"

void Non_Member_Func(A &a)
{
a.W *= 2; // Can access public
a.X *= 2; // can access public
a.Y *= 2; // can access private
a.Z *= 2; // can access private

printf("W: %d\nX: %d\nY: %d\nZ: %d\n", a.W, a.X, a.Y, a.Z);
}

void Member_Func(A &a)
{
a.W *= 2;
a.X *= 2;

// a.Y += 4; // Error can't access private
// a.Z += 6; // Error can't access private

printf("W: %d\nX: %d\n", a.W, a.X);
}

void Member_Func2(A &a)
{
a.W *= 4;
a.X *= 4;

// a.Y += 4; // Error can't access private
// a.Z += 6; // Error can't access private

printf("W: %d\nX: %d\n", a.W, a.X);
}

int main(void)
{
A a;

Non_Member_Func( a );

a.Set_Func_Ptr( Member_Func );
a.Run();

a.Set_Func_Ptr( Member_Func2) ;
a.Run();

return 0;
}
 
Reply With Quote
 
 
 
 
HL
Guest
Posts: n/a
 
      07-06-2008
On 7月6日, 上午7时38分, Immortal Nephi <Immortal_Ne....@satx.rr.com> wrote:
> I design a class for general purpose. I do not allow a client to read
> or modify interface and implemention. I allow them to write a new non-
> member function outside of class' interface and implmention. The
> problem is that non-member function cannot access private data member.
>
> The friend keyword is not the solution. I am aware that friend
> keyword allows the non-member function to access private data member.
>
> If you define more than two non-member functions, you will want one of
> some non-member function to be pointed to the member function pointer
> at run-time. The client can decide to remove first non-member
> function from the member function pointer and add another non-member
> function at run-time. The member function pointer array is not
> necessary according to my design.
>
> Does it mean that it is the only solution to access public data member
> only?
>
> Take a look at my example code below.
>
> #ifndef A_H
> #define A_H
>
> class A
> {
> public:
> typedef void (*Func_Ptr)(A &);
>
> A(void);
> ~A(void);
>
> void Run(void);
> void Set_Func_Ptr(Func_Ptr);
>
> private:
> friend void Non_Member_Func(A &);
>
> Func_Ptr Member_Func_Ptr;
>
> public:
> int W;
> int X;
>
> private:
> int Y;
> int Z;
>
> };
>
> #endif // A_H
>
> // A_CPP
> #include "A.h"
>
> A::A(void)
> {
> W = 1;
> X = 2;
> Y = 3;
> Z = 4;
>
> }
>
> A::~A(void)
> {
>
> }
>
> void A::Run(void)
> {
> Non_Member_Func( *this );
> Member_Func_Ptr( *this );
>
> }
>
> void A::Set_Func_Ptr(Func_Ptr Ptr)
> {
> Member_Func_Ptr = Ptr;
>
> }
>
> // MAIN_CPP
> #include <stdio.h>
> #include "A.h"
>
> void Non_Member_Func(A &a)
> {
> a.W *= 2; // Can access public
> a.X *= 2; // can access public
> a.Y *= 2; // can access private
> a.Z *= 2; // can access private
>
> printf("W: %d\nX: %d\nY: %d\nZ: %d\n", a.W, a.X, a.Y, a.Z);
>
> }
>
> void Member_Func(A &a)
> {
> a.W *= 2;
> a.X *= 2;
>
> // a.Y += 4; // Error can't access private
> // a.Z += 6; // Error can't access private
>
> printf("W: %d\nX: %d\n", a.W, a.X);
>
> }
>
> void Member_Func2(A &a)
> {
> a.W *= 4;
> a.X *= 4;
>
> // a.Y += 4; // Error can't access private
> // a.Z += 6; // Error can't access private
>
> printf("W: %d\nX: %d\n", a.W, a.X);
>
> }
>
> int main(void)
> {
> A a;
>
> Non_Member_Func( a );
>
> a.Set_Func_Ptr( Member_Func );
> a.Run();
>
> a.Set_Func_Ptr( Member_Func2) ;
> a.Run();
>
> return 0;
>
> }

I don't know if I understand your purpose to design such a class. It's
weird to allow client to write a non-member function to access private
member of a general purpose interface class. If you design a class for
general purpose, you may only make interface ( or public member )
exposed to client users rather than implementation ( private member).
If the user can write non-member to directly access private member,
why C++ need access control? except you use some trickery by access
its memory directly.


 
Reply With Quote
 
 
 
 
Erik Wikstr枚m
Guest
Posts: n/a
 
      07-06-2008
On 2008-07-06 01:38, Immortal Nephi wrote:
> I design a class for general purpose. I do not allow a client to read
> or modify interface and implemention. I allow them to write a new non-
> member function outside of class' interface and implmention. The
> problem is that non-member function cannot access private data member.
>
> The friend keyword is not the solution. I am aware that friend
> keyword allows the non-member function to access private data member.
>
> If you define more than two non-member functions, you will want one of
> some non-member function to be pointed to the member function pointer
> at run-time. The client can decide to remove first non-member
> function from the member function pointer and add another non-member
> function at run-time. The member function pointer array is not
> necessary according to my design.
>
> Does it mean that it is the only solution to access public data member
> only?


I'm not at all sure what you are trying to accomplish. If you want the
user to be able to add functionality to your class without changing it
you should declare the relevant member protected and allow the user to
derived from it.

--
Erik Wikstr枚m
 
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
diffrence between "function pointer" and "pointer to a function" murgan C Programming 6 12-21-2005 06:01 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Can I use a C++ function pointer instead of a C one Franz C++ 4 10-20-2003 08:31 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM



Advertisments
 



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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57