sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Mad Ant's Avatar

pointer to function in constructor


Question posted by: Mad Ant (Guest) on August 21st, 2008 03:35 PM
Hello all.

I have to convert a program from C to C++. The original code used
drivers that were available in C, and now are only available in C++.
Thus, I have the "honour" of converting the code.
There are a couple of areas that are blocking me. I've already taken
care of the rest.

I have a structure that is initialized as soon as it is declared in a
variable :
<<
const struct driver_t beirlistener_driver = {
.name = "beirlistener_driver",
.cfg_ctl = {
.cfg_new = &beirlistener_cfg_new,
.cfg_parse = &beirlistener_cfg_parse,
.cfg_del = &beirlistener_cfg_del,
.cfg_parse_ended = &beirlistener_cfg_parse_end
},
.ctx_ctl = {
.ctx_new = &beirlistener_ctx_new,
.ctx_del = &beirlistener_ctx_del
},
.thread_ctl = {
.run = &beirlistener_run,
.queue_clean = NULL,
.stop = &thread_request_stop_intr
}
};
Quote:
Originally Posted by
Quote:
Originally Posted by
>>

name is, of course, a 'char *'.
cfg_ctl, ctx_ctl and thread_ctl are structures containing pointers to
functions.
The structure contains the necessary functions called to initialize,
un, and destroy/delete a thread. It is used in several different
files, depending on the thread it has to start.

Of course, it would be folly to suppose that it would be accepted as
is in C++. It's not.
The only real solution I could find was to declare a constructor for
the struct (not really elegant, I know), which would receive the
addresses of the functions that are called. So the variable
declaration would be something along the lines of :
<<
const struct driver_t beirlistener_driver = {"beirlistener_driver",
&beirlistener_cfg_new, &beirlistener_cfg_parse, ...};
Quote:
Originally Posted by
Quote:
Originally Posted by
>>

However, I tried declaring it as such ... :
<<
struct driver_t {
char * name;
struct config_ctl_t cfg_ctl;
struct context_ctl_t ctx_ctl;
struct thread_ctl_t thread_ctl;

driver_t(char * cname, int (*fcn)(void **)) {
name = cname;
cfg_ctl.cfg_new = fcn;
}
};
Quote:
Originally Posted by
Quote:
Originally Posted by
>>

... along with the variable declaration as mentioned just above, but
it doesn't work. It doesn't seem to recognize the pointer to a
function in the constructor, since it gives me the following message :
<<
/home/anton/Projects/splusserver3/src/beirlistener.c:90: error: scalar
object ‘beirlistener_driver’ requires one element in initializer
Quote:
Originally Posted by
Quote:
Originally Posted by
>>


I've done research, but I con't find a solution to this.
Any help anyone can give me to resolve this is very much appreciated.
Thanks all.

Anton
3 Answers Posted
mlimber's Avatar
Guest - n/a Posts
#2: Re: pointer to function in constructor

On Aug 21, 10:29*am, Mad Ant <anton.har...@gmail.comwrote:
Quote:
Originally Posted by
I have to convert a program from C to C++. The original code used
drivers that were available in C, and now are only available in C++.
Thus, I have the "honour" of converting the code.
There are a couple of areas that are blocking me. I've already taken
care of the rest.
>
I have a structure that is initialized as soon as it is declared in a
variable :
<<
const struct driver_t beirlistener_driver = {
* * * * .name = "beirlistener_driver",
* * * * .cfg_ctl = {
* * * * * * * * .cfg_new = &beirlistener_cfg_new,
* * * * * * * * .cfg_parse = &beirlistener_cfg_parse,
* * * * * * * * .cfg_del = &beirlistener_cfg_del,
* * * * * * * * .cfg_parse_ended = &beirlistener_cfg_parse_end
* * * * },
* * * * .ctx_ctl = {
* * * * * * * * .ctx_new = &beirlistener_ctx_new,
* * * * * * * * .ctx_del = &beirlistener_ctx_del
* * * * },
* * * * .thread_ctl = {
* * * * * * * * .run = &beirlistener_run,
* * * * * * * * .queue_clean = NULL,
* * * * * * * * .stop = &thread_request_stop_intr
* * * * }};
>
name is, of course, a 'char *'.
cfg_ctl, ctx_ctl and thread_ctl are structures containing pointers to
functions.
The structure contains the necessary functions called to initialize,
un, and destroy/delete a thread. It is used in several different
files, depending on the thread it has to start.
>
Of course, it would be folly to suppose that it would be accepted as
is in C++. It's not.
The only real solution I could find was to declare a constructor for
the struct (not really elegant, I know), which would receive the
addresses of the functions that are called. So the variable
declaration would be something along the lines of :
<<
const struct driver_t beirlistener_driver = {"beirlistener_driver",
&beirlistener_cfg_new, &beirlistener_cfg_parse, ...};
>
However, I tried declaring it as such ... :
<<
struct driver_t {
* * * * char * * * * * * * * * ** * * *name;
* * * * struct config_ctl_t * * * * * * cfg_ctl;
* * * * struct context_ctl_t * * * * * *ctx_ctl;
* * * * struct thread_ctl_t * * * * * * thread_ctl;
>
* * * * driver_t(char * cname, int (*fcn)(void **)) {
* * * * * * * * name = cname;
* * * * * * * * cfg_ctl.cfg_new = fcn;
* * * * }};
>
... along with the variable declaration as mentioned just above, but
it doesn't work. It doesn't seem to recognize the pointer to a
function in the constructor, since it gives me the following message :
<<
/home/anton/Projects/splusserver3/src/beirlistener.c:90: error: scalar
object ‘beirlistener_driver’ requires one element in initializer
>
>
>
I've done research, but I con't find a solution to this.
Any help anyone can give me to resolve this is very much appreciated.
Thanks all.


Try:

const struct driver_t beirlistener_driver = {
"beirlistener_driver",
{
&beirlistener_cfg_new,
&beirlistener_cfg_parse,
&beirlistener_cfg_del,
&beirlistener_cfg_parse_end
},
{
&beirlistener_ctx_new,
&beirlistener_ctx_del
},
{
&beirlistener_run,
NULL,
&thread_request_stop_intr
}
};

Cheers! --M
Mad Ant's Avatar
Guest - n/a Posts
#3: Re: pointer to function in constructor

On Aug 21, 4:49 pm, mlimber <mlim...@gmail.comwrote:
Quote:
Originally Posted by
On Aug 21, 10:29 am, Mad Ant <anton.har...@gmail.comwrote:
>
>
>
Quote:
Originally Posted by
I have to convert a program from C to C++. The original code used
drivers that were available in C, and now are only available in C++.
Thus, I have the "honour" of converting the code.
There are a couple of areas that are blocking me. I've already taken
care of the rest.

>
Quote:
Originally Posted by
I have a structure that is initialized as soon as it is declared in a
variable :
<<
const struct driver_t beirlistener_driver = {
.name = "beirlistener_driver",
.cfg_ctl = {
.cfg_new = &beirlistener_cfg_new,
.cfg_parse = &beirlistener_cfg_parse,
.cfg_del = &beirlistener_cfg_del,
.cfg_parse_ended = &beirlistener_cfg_parse_end
},
.ctx_ctl = {
.ctx_new = &beirlistener_ctx_new,
.ctx_del = &beirlistener_ctx_del
},
.thread_ctl = {
.run = &beirlistener_run,
.queue_clean = NULL,
.stop = &thread_request_stop_intr
}};

>
Quote:
Originally Posted by
name is, of course, a 'char *'.
cfg_ctl, ctx_ctl and thread_ctl are structures containing pointers to
functions.
The structure contains the necessary functions called to initialize,
un, and destroy/delete a thread. It is used in several different
files, depending on the thread it has to start.

>
Quote:
Originally Posted by
Of course, it would be folly to suppose that it would be accepted as
is in C++. It's not.
The only real solution I could find was to declare a constructor for
the struct (not really elegant, I know), which would receive the
addresses of the functions that are called. So the variable
declaration would be something along the lines of :
<<
const struct driver_t beirlistener_driver = {"beirlistener_driver",
&beirlistener_cfg_new, &beirlistener_cfg_parse, ...};

>
Quote:
Originally Posted by
However, I tried declaring it as such ... :
<<
struct driver_t {
char * name;
struct config_ctl_t cfg_ctl;
struct context_ctl_t ctx_ctl;
struct thread_ctl_t thread_ctl;

>
Quote:
Originally Posted by
driver_t(char * cname, int (*fcn)(void **)) {
name = cname;
cfg_ctl.cfg_new = fcn;
}};

>
Quote:
Originally Posted by
... along with the variable declaration as mentioned just above, but
it doesn't work. It doesn't seem to recognize the pointer to a
function in the constructor, since it gives me the following message :
<<
/home/anton/Projects/splusserver3/src/beirlistener.c:90: error: scalar
object ‘beirlistener_driver’ requires one element in initializer

>
Quote:
Originally Posted by
I've done research, but I con't find a solution to this.
Any help anyone can give me to resolve this is very much appreciated.
Thanks all.

>
Try:
>
const struct driver_t beirlistener_driver = {
"beirlistener_driver",
{
&beirlistener_cfg_new,
&beirlistener_cfg_parse,
&beirlistener_cfg_del,
&beirlistener_cfg_parse_end
},
{
&beirlistener_ctx_new,
&beirlistener_ctx_del
},
{
&beirlistener_run,
NULL,
&thread_request_stop_intr
}
};
>
Cheers! --M


This works perfectly. I can't believe I didn't think of it. *hits
himself on the forehead*

Cheers!

Mad Ant
Old Wolf's Avatar
Guest - n/a Posts
#4: Re: pointer to function in constructor

On Aug 22, 2:29 am, Mad Ant <anton.har...@gmail.comwrote:
Quote:
Originally Posted by
Hello all.
>
I have to convert a program from C to C++. The original code used
drivers that were available in C, and now are only available in C++.
Thus, I have the "honour" of converting the code.


Are you aware that you can mix C and C++
within a project ? If you have working C
code, then you do not need to change it;
you can add C++ files to the project and
still link the objects together.

 
Not the answer you were looking for? Post your question . . .
197,049 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,049 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors