Thread: Problem with function pointer

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    4

    Question Problem with function pointer

    For example : in class A there is a public function pointer to the function B. How make function B can access to the private members or public members of class A; Code :
    Code:
    class A
    void B1 ()
    {
        Item_ = 10;
        TestMemberF();
        std::cout << "B Function Workd" << std::endl;
    }
    
    class A1
    {
        private :
            void func1()
            {
                std::cout << "F1 works" << std::endl;
            }
            void func2()
            {
                std::cout << "F2 works" << std::endl;
            }
            int Item_ = 5;
            void (*ArgFunc)();
            void TestMemberF() { std::cout << "Test member func";}
        public :
            void DefineFunc(void (*Arg)())
            {
                ArgFunc = Arg;
            }
            void ExecutFunc()
            {
                ArgFunc();
            }
    
    
    };
    How to make function B can access to TestMember and Item_?
    Last edited by XioNOverMazes; 05-13-2015 at 03:08 AM.

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    I can't see the implementation of 'class A' so I assume you are talking about 'class A1'.

    For a function to access the private variables of a class, it has to be a friend to the class.

    Code:
     
    class A1
    {
        friend void B1(A1 &);
    
    }
    
    /* in function B1 */
    
    void B1(A1 &rhs)
    { 
         rhs.something = /* blah blah */
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function and Pointer problem
    By austin.18 in forum C Programming
    Replies: 4
    Last Post: 11-03-2014, 11:16 AM
  2. Help with pointer to function problem
    By rob90 in forum C Programming
    Replies: 19
    Last Post: 05-28-2010, 10:33 AM
  3. Pointer to function problem
    By vincent01910 in forum C Programming
    Replies: 6
    Last Post: 08-29-2009, 08:14 AM
  4. Pointer in Function problem
    By lilhawk2892 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 02:41 AM
  5. Replies: 4
    Last Post: 11-05-2006, 02:57 PM