473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Implementing callback function with pure virtual function

108 100+
Hi guys,
I know function pointers but I'm having a hard time about callback function when it is associated with abstract class. I have an example code below which I got from a book. Kindly explain it to me and how will can I called and implement the callback function.

ImageReader.h

class MImageReadyCallBack
{
public:
virtual void ImageReadyL(const TInt& aError) = 0;
};

class CImage_Reader : public CActive
{
public:
CImage_Reader(MImageReadyCallBack& aNotify);
}

In this example, how can will I implement int the .cpp the CImage_Reader constructor since it needs a callback function of type MImageReadyCallBack?Where will I implement the ImageReadyL functions? Do I need to inherit MImageReadyCallBack in the declaration of CImage_Reader class?

Hope you can explain it to me. I will try to research while waiting on your reply. Thank you guys...
Dec 19 '07 #1
5 4759
gpraghuram
1,275 Expert 1GB
Hi guys,
I know function pointers but I'm having a hard time about callback function when it is associated with abstract class. I have an example code below which I got from a book. Kindly explain it to me and how will can I called and implement the callback function.

ImageReader.h

class MImageReadyCallBack
{
public:
virtual void ImageReadyL(const TInt& aError) = 0;
};

class CImage_Reader : public CActive
{
public:
CImage_Reader(MImageReadyCallBack& aNotify);
}

In this example, how can will I implement int the .cpp the CImage_Reader constructor since it needs a callback function of type MImageReadyCallBack?Where will I implement the ImageReadyL functions? Do I need to inherit MImageReadyCallBack in the declaration of CImage_Reader class?

Hope you can explain it to me. I will try to research while waiting on your reply. Thank you guys...

Hi,
The virtual concept works like this.
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4. virtual void fun(){}=0;
  5. };
  6. class B:public A
  7. {
  8. void fun()
  9. {
  10. cout<<"in b"<<endl;
  11. }
  12. };
  13.  
  14. int main()
  15. {
  16. A * a1 = new B();
  17. a1->fun();//will print in b
  18. }
  19.  
So if you want the virtual to work then you should derive from the base class where the method is virtua.
Hope my example helps you

Raghuram
Dec 19 '07 #2
romcab
108 100+
Hi,
The virtual concept works like this.
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4. virtual void fun(){}=0;
  5. };
  6. class B:public A
  7. {
  8. void fun()
  9. {
  10. cout<<"in b"<<endl;
  11. }
  12. };
  13.  
  14. int main()
  15. {
  16. A * a1 = new B();
  17. a1->fun();//will print in b
  18. }
  19.  
So if you want the virtual to work then you should derive from the base class where the method is virtua.
Hope my example helps you

Raghuram
Hi,

Thanks for your quick reply. I understand very well your example and it refresh my memory about virtual and polymorphism. In my example which I got from a book, in creating an object I need to pass a reference of type abstract which in turn will call the callback function. I got this class to this site.

http://wiki.forum.nokia.com/index.php/How_to_read_images_to_Symbian_bitmap
Dec 19 '07 #3
gpraghuram
1,275 Expert 1GB
Hi,

Thanks for your quick reply. I understand very well your example and it refresh my memory about virtual and polymorphism. In my example which I got from a book, in creating an object I need to pass a reference of type abstract which in turn will call the callback function. I got this class to this site.

http://wiki.forum.nokia.com/index.php/How_to_read_images_to_Symbian_bitmap

Hi,
I think you can use that way...
If my understanding is right then see my example below, which hellp you.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A
  6. {
  7.     public:
  8.     virtual void fun()=0;
  9. };
  10. class B:public A
  11. {
  12.     void fun()
  13.     {
  14.         cout<<"in b"<<endl;
  15.     }
  16. };
  17.  
  18. class Wrap
  19. {
  20.     public:
  21.     void fun_wrap(A &_ipobj)
  22.     {
  23.         _ipobj.fun();
  24.     }
  25. };
  26.  
  27. int main()
  28. {
  29.     A * a1 = new B();
  30.     //a1->fun();//will print in b
  31.     Wrap *w1 = new Wrap();
  32.     w1->fun_wrap(*a1);
  33. }
  34.  
  35.  
If i hvent got your requirements properly post again
Raghuram
Dec 19 '07 #4
romcab
108 100+
Hi,
I think you can use that way...
If my understanding is right then see my example below, which hellp you.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class A
  6. {
  7.     public:
  8.     virtual void fun()=0;
  9. };
  10. class B:public A
  11. {
  12.     void fun()
  13.     {
  14.         cout<<"in b"<<endl;
  15.     }
  16. };
  17.  
  18. class Wrap
  19. {
  20.     public:
  21.     void fun_wrap(A &_ipobj)
  22.     {
  23.         _ipobj.fun();
  24.     }
  25. };
  26.  
  27. int main()
  28. {
  29.     A * a1 = new B();
  30.     //a1->fun();//will print in b
  31.     Wrap *w1 = new Wrap();
  32.     w1->fun_wrap(*a1);
  33. }
  34.  
  35.  
If i hvent got your requirements properly post again
Raghuram
Hi..

Thanks...I can understand your point. Does it mean that the example class in the links above is not sufficient since I still need to add another class? If no other way, then I think I need to follow your logic.
Dec 19 '07 #5
gpraghuram
1,275 Expert 1GB
Hi..

Thanks...I can understand your point. Does it mean that the example class in the links above is not sufficient since I still need to add another class? If no other way, then I think I need to follow your logic.

Maybe you post the code whatever u have tried and let me take a look into it.

Raghuram
Dec 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
6
by: ma740988 | last post by:
I was perusing modern C++ designs and the loki library and I might add I hit overload when I hit select part of that book and the library. The template _stuff_ is very intense ...... whew!! Oso...
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
2
by: Marcus Kwok | last post by:
I have processing code (I'll call it the "model") written in native unmanaged pure C++, and I have put a GUI on top of it written using Windows Forms (.NET 1.1). The GUI is used to set the...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK...
3
by: Nashirak | last post by:
I am trying to implement callbacks in C++. I am modeling my callbacks after some of the stuff that was done in wxWidgets (if anyone is familiar with that). The syntax is as follows: // Header...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.