0

I have a c++ program that imports a dll with lots of functions.

//.h file

/*Description: set velocity(short id of axis, double velocity value)*/
typedef short(__stdcall *GT_SetVel)(short profile, double vel);
//.cpp file

/*Description: set velocity(short id of axis, double velocity value)*/
GT_SetVel SetAxisVel = NULL;
...
SetAxisVel = (GT_SetVel)GetProcAddress(GTDLL, "_GT_SetVel@10");
...
SetAxisVel(idAxis, vel);

I want to make it more compact, like

//.h file

/*Description: set velocity(short id of axis, double velocity value)*/
typedef short(__stdcall *GT_SetVel)(short profile, double vel) SetAxisVel = NULL;
//.cpp file

SetAxisVel = (GT_SetVel)GetProcAddress(GTDLL, "_GT_SetVel@10");
...
SetAxisVel(idAxis, vel);

It may sound ridiculous. Is there a syntax similar to the above, where two statements are merged into one and not just placed together into ajacent lines.

The reason is that
(1) I need both the type alias and the function pointer variable,
(2) and it's necessary to have the description comment for both typedef (semantic to have argument description by argument list) and pointer declaration (provide intellisense for later use).

But the type alias is only used once, it seems redundant to insert the same description in two seperate places.

Is there a way to make it more compact? Thanks.

2
  • As I understood you want to declare variable with typedef'ed type and initialize it by one declaration. No you can't handle it with typedef. But if you variable is static or external you don't need initialize it. Commented Aug 14, 2017 at 7:38
  • A type alias and a variable are different kind of elements. I don't see any problem with inserting two different comments to each. Commented Aug 14, 2017 at 7:53

1 Answer 1

1

By getting rid of typedef, you may shorten to:

// .cpp

/*Description: set velocity(short id of axis, double velocity value)*/
short(__stdcall *SetAxisVel)(short profile, double vel) = NULL;


SetAxisVel = reinterpret_cast<decltype(SetAxisVel)>(GetProcAddress(GTDLL, "_GT_SetVel@10"));
Sign up to request clarification or add additional context in comments.

4 Comments

But there is a problem defining variables in header files which results in multiple definition ( LNK2005 ) errors.
Do you need to place it in header ? Your first snippet doesn't.
I just want to enclose all the descriptions in a seperate file.
"the type alias is only used once", So I get rid of that, so all code is in the same cpp file now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.