Solved

C++ function pointer initialisation

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

25 Experts available now in Live!

Get 1:1 Help Now