1

im working on converting C code to c++ , in one section of the code i have something like function array in C that looks like:

this is how C looks like :

void func1(bool a)
{
..
}

void func2(bool a)
{
..
}

void func3(bool a)
{
..
}


struct func
{
    void (*f)(incoming *);
    int arg_length;
};
typedef struct func func;

func funcs[] = {
        { func1, 4 }, 
        { func2, 10 }, 
        { func3, 4 }  
    };

how can it converted to c++?
UPDATE:
question: is this is valid answer for none static function pointers ?
http://www.newty.de/fpt/fpt.html#chapter2

also can i define Array of different types of member function pointer?

user63898
  • 29,839
  • 85
  • 272
  • 514
  • 6
    This is already valid C++. – Oliver Charlesworth Jul 21 '12 at 09:55
  • Also, what you expect to do with `funcs`? Aren't you storing the length of `funcs` somewhere, or obtaining it using hackery? Because I don't see sentinel in it. Also, I think `typedef` is not necessary in C++ because structs (alike classes) can be accessed by name. – Michał Górny Jul 21 '12 at 09:58
  • Ah, and now I see that some scary argument hackery is done here as well. Where is `incoming` declared, and are you just hardcoding argument sizes for your platform? – Michał Górny Jul 21 '12 at 10:01
  • Sentinels aren't always necessary; I usually have a macro ARRAYSIZE() (or CSARRAYSIZE() nowadays) that calculates the number of elements. Depending on what the code actually does, just using 3 as a literal might even be ok as well, like when you have an array indexed by digits you know that there are 10 entries. – Christian Stieber Jul 21 '12 at 10:10
  • 1
    Actually, I take that back. This isn't even valid C! (Also, a need to assign functions to function-pointers with a different signature is usually a sign of a design that needs rethinking.) – Oliver Charlesworth Jul 21 '12 at 10:15
  • This can't be answered without knowing what you intend to do after converting the function pointers to the wrong type. The conversions *could* be forced with `reinterpret_cast`, but there's almost certainly a better solution to the actual problem you want to solve. – Mike Seymour Jul 21 '12 at 10:28
  • 1
    @OliCharlesworth: In fact, arbitrary function pointer conversions *are* allowed in C. Of course, it's undefined behaviour to call one of the wrong type, so it's hard to imagine how this code could do anything sensible. – Mike Seymour Jul 21 '12 at 10:36
  • @MikeSeymour: Indeed, but AFAIK they need an explicit cast, which the above code doesn't have... – Oliver Charlesworth Jul 21 '12 at 10:36
  • @OliCharlesworth: No, no cast is needed. C99 6.3.2.3/8 says "A pointer to a function of one type may be converted to a pointer to a function of another type and back again." [and in the introduction to that subclause, 6.3/1 says "This subclause specifies the result required from such an *implicit conversion*"] – Mike Seymour Jul 21 '12 at 10:40
  • @MikeSeymour: and then that sentence continues with "as well as those that result from a cast operation (an explicit conversion)"! I don't know of a compiler that would allow the above code without a warning. – Oliver Charlesworth Jul 21 '12 at 10:42
  • @OliCharlesworth: Yes, maybe it's more ambiguous than I thought. I was fooled by the fact that my compiler (and presumably the OP's) accepted the conversions, and can't really be bothered to dig further into the standard to find out whether it should or not. – Mike Seymour Jul 21 '12 at 10:55
  • 1
    @MikeSeymour: Actually, I guess we can infer that C++ explicitly forbids the above code, as g++ reports this as a flat-out error. So yes, it sounds like you're right that C++ is more stringent than C. – Oliver Charlesworth Jul 21 '12 at 11:01
  • 1
    @OliCharlesworth: Indeed, C++ definitely doesn't allow arbitrary pointer conversions like these. – Mike Seymour Jul 21 '12 at 11:02

1 Answers1

-1

You could define a class, and insert a '[]' operator to get pointers to functions, in your case.

alinsoar
  • 15,386
  • 4
  • 57
  • 74
  • 1
    This is one case where valid C is not valid C++. The conversion from `void(bool)` to `void(*)(incoming*)` is allowed in C, but not in C++, so you do have to do something to convert it to C++. – Mike Seymour Jul 21 '12 at 10:22
  • void (*f)(incoming *) is a pointer to a function returning nothing, and taking as parameter a pointer to a structure 'incoming. i see nothin wrong in c++. – alinsoar Jul 21 '12 at 10:28
  • 1
    The implicit conversion from a different function type, `void(bool)`, is not allowed in C++. So the initialisation of a variable of type `void(*f)(incoming*)` from `func1` is an error. – Mike Seymour Jul 21 '12 at 10:29
  • "The C language is embedded in C++" - http://stackoverflow.com/questions/10461331 – skink Jul 21 '12 at 11:05
  • It isn't a correct answer for this case, though c can usually be mapped to cpp – Jordan Mackie Apr 28 '19 at 03:14