473,794 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing class member functions as a parameter to a non-class function.

1 New Member
I'm an old Delphi programmer getting into the C++ world, and trying to wrap my head around this problem.

I have multiple classes derived from a generic class that passes one of it's functions as a parameter to another function. The code is very complex, so I've written this abridged version of what I am trying to do.

Expand|Select|Wrap|Line Numbers
  1. // genClass.h
  2. #ifndef genClassH
  3. #define genClassH
  4.  
  5. #include <Classes.hpp>
  6.  
  7. // Class declerations
  8. class genClass
  9. {
  10.   virtual char *doStuff(char *) = 0;
  11.   char *doIt(char *,char *);
  12. };
  13.  
  14. class getFive : genClass
  15. {
  16.   char *doStuff(char *input);
  17. };
  18.  
  19. class getThree : genClass
  20. {
  21.   char *doStuff(char *input);
  22. };
  23.  
  24. // Function
  25. char *genFunc(char *,char *(*func)(char *));
  26.  
  27. #endif
Expand|Select|Wrap|Line Numbers
  1. // genClass.cpp
  2. #pragma hdrstop
  3. #include "genClass.h"
  4. #pragma package(smart_init)
  5.  
  6. // Class member definitions
  7. char *getFive::doStuff(char *input)
  8. {
  9.   char *result;
  10.   strncpy(result,input,5);
  11.   return result;
  12. }
  13.  
  14. char *getThree::doStuff(char *input)
  15. {
  16.   char *result;
  17.   strncpy(result,input,3);
  18.   return result;
  19. }
  20.  
  21. char *genClass::doIt(char *pre,char *str)
  22. {
  23.   return genFunc(pre,doStuff(str));
  24. }
  25.  
  26. // Function definition
  27. char *genFunc(char *pre,char *(*func)(char *input))
  28. {
  29.   char *result;
  30.   strcpy(result,pre);
  31.   strcat(result,func);
  32.   return result;
  33. }
I'm trying to compile this in Borland C++ Builder. I think I've overly confused myself, but this is where I'm stuck.

My error message is:
[C++ Error] genClass.cpp(23 ): E2034 Cannot convert 'char *' to 'char * (*)(char *)'

TIA for any help.
Aug 17 '07 #1
1 1586
weaknessforcats
9,208 Recognized Expert Moderator Expert
Several things:

1)There are no public/private specifiers. You will probably need these.

2) This code:
char *getFive::doStu ff(char *input)
{
char *result;
strncpy(result, input,5);
return result;
}
does not allocate memory for result before copying data to that location. Guranteed program crash here. Other places have the same problem.

3) This code:
char *genClass::doIt (char *pre,char *str)
{
return genFunc(pre,doS tuff(str));
}
does a call to doStuff(str). That call returns a char*. The argument is supposed to be a function pointer. Hence, the compiler bark about not being able to convert a char* to a function pointer.

4) What are you trying to do??
The way these classes are set up and used looks very incorrect.
Aug 17 '07 #2

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

Similar topics

1
7790
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains: ---------------------------------------------------------------------------- <?php ini_set("session.use_cookies", "off"); ini_set("session.use_trans_sid", "on"); session_start(); $_SESSION = ""; $_SESSION = ""; echo "<form method='POST' action='login.php'>
13
2445
by: Erik Haugen | last post by:
From reading gotw#84 (http://www.gotw.ca/gotw/084.htm), I'm convinced that I should try to make functions nonfriend nonmembers when practical, but then I came across this: Bruce Eckel says about operator overloads in Thinking In C++ (2nd Ed - http://www.codeguru.com/cpp/tic/tic0129.shtml) "In general, if it doesn't make any difference, they should be members, to emphasize the association between the operator and its class." That seems...
5
2763
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator erase(iterator first, iterator last); size_type erase(const Key& keyval); Ie, the first two functions above have the same interface as
10
125462
by: Resant | last post by:
I have a query : Exec 'Select * From Receiving Where Code In (' + @pCode + ')' @pCode will contain more than one string parameter, eg : A1, A2, A3 How can i write that parameters, I try use : set @pCode='A1','A2','A3' but get an error : Incorrect syntax near ','
5
1654
by: blue | last post by:
We often get connection pooling errors saying that there are no available connections in the pool. I think the problem is that we are passing around open readers all over the place. I am planning on changing this in our code and I expect this to fix our problem. We have our connection pooling set to the default number of connections open. We probably have about 3-7 users concurrently using our web site. So, the problem isn't that we...
9
3780
by: tkrogc | last post by:
I compiled the source below using the comeau compiler http://www.comeaucomputing.com/tryitout/ Why does the f funtion compile while the g function produces an lvalue error ? (I thought the two functions were identical except for a harmless syntax difference) struct A { void f(){}
4
1356
by: =?ISO-8859-15?Q?Sven_K=F6hler?= | last post by:
Hi, look at this example: struct T1 { bool v; T1(bool p = false) { this->v = p; }
11
4146
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my mind) and it would be fairly generic. The only sidecase I can see is that operator. itself would not be looked up through operator. . I read that there was previous debate on the subject, but I haven't been able to find why it was rejected.
18
3274
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
1
2258
by: tarunkhatri | last post by:
Hi, I want to pass the parameter of employee_id to a page. My code is working fine and passing parameter to page but the problem is rather then the current parameter. It passed the parameter which was selected in previous submit. Below is the code. <form action='manager_employee_select.php?proc_employee_id=<?php echo $_POST; ?>' method="POST"> <table> <tr> My team members timesheet</tr> <tr>Select a team member from the list to...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.