Solved

C++ function pointer initialisation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 160
VBSCRIPT string remove all characters to the right after extension 4 53
Advice in Xamarin 21 56
T-SQL:  Sigh---Boy, this is fun.... 12 32
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now