Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ function pointer initialisation

Posted on 2014-04-05
3
Medium Priority
?
598 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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…

688 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