Google Groups Home
Help | Sign in
Use Function Pointer Instead Friend?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Immortal Nephi  
View profile
 More options Jul 5, 7:38 pm
Newsgroups: comp.lang.c++
From: Immortal Nephi <Immortal_Ne...@satx.rr.com>
Date: Sat, 5 Jul 2008 16:38:31 -0700 (PDT)
Local: Sat, Jul 5 2008 7:38 pm
Subject: Use Function Pointer Instead Friend?
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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
HL  
View profile
 More options Jul 5, 8:50 pm
Newsgroups: comp.lang.c++
From: HL <moogle.l...@gmail.com>
Date: Sat, 5 Jul 2008 17:50:40 -0700 (PDT)
Local: Sat, Jul 5 2008 8:50 pm
Subject: Re: Use Function Pointer Instead Friend?
On 7月6日, 上午7时38分, Immortal Nephi <Immortal_Ne...@satx.rr.com> wrote:

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Erik Wikström  
View profile
 More options Jul 6, 5:25 am
Newsgroups: comp.lang.c++
From: Erik Wikström <Erik-wikst...@telia.com>
Date: Sun, 06 Jul 2008 09:25:37 GMT
Local: Sun, Jul 6 2008 5:25 am
Subject: Re: Use Function Pointer Instead Friend?
On 2008-07-06 01:38, Immortal Nephi wrote:

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    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google