Solved

C++ function pointer initialisation

Posted on 2014-04-05
3
587 Views
Last Modified: 2014-04-05
I am having trouble initialising a function pointer using Boland C++ builder.

A sample of code is given below. I can declare a function pointer but my attempts to code its initialisation have not succeeded.

HEADER File:

typedef class C1
{
    public:
    C1();
    void test(char);

}C1;

typedef void(C1::*fnptr)(char);

//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:      // IDE-managed Components
private:      // User declarations
    fnptr fp;  // I haven't used this yet
    C1 *aclass;
public:            // User declarations
    __fastcall TForm1(TComponent* Owner);
};

CPP FILE:

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

C1::C1()
{

}
void C1::test(char x)
{

}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    myclass = new C1();
    void (C1::*p)(char); // declare a pointer to a C1::test(char) function

    p = ?? // how do I code the address of the myclass->test(char) function

}
//---------------------------------------------------------------------------
0
Comment
Question by:alcindor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Author Comment

by:alcindor
ID: 39980575
Correction in the definition of TForm1 class

class TForm1 : public TForm
{
__published:      // IDE-managed Components
private:      // User declarations
    fnptr fp;  // I haven't used this yet
    C1 *myclass;  //     <----  this is the correction
public:            // User declarations
    __fastcall TForm1(TComponent* Owner);
};
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39980599
There is a little misconception: The function pointer you are using is not 'myclass->test(()', but 'C1::test()' which is later bound to an instance of C2 when you call it. That would thenbe like

  void (C1::*p)(char); // declare a pointer to a C1::test(char) function

    p = C1::test;

Open in new window


You would later use that pointer like

  void (C1::*p)(char); // declare a pointer to a C1::test(char) function

    p = C1::test;

    // ...

    C1 c1;

    (c1.*p)('a');

Open in new window


See also http://www.newty.de/fpt/index.html ("The Function Pointer Tutorials")
0
 
LVL 2

Author Closing Comment

by:alcindor
ID: 39980617
Thanks for your excellent solution.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question