Solved

C++ function pointer initialisation

Posted on 2014-04-05
3
582 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 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