Connect two third party modules with “const char*” and “char*” argumentsHow to convert a std::string to const char* or char*?Convert const char* type to int arrayWhat is the difference between using Str.c_str() directly and use it to initialize a const char *?static_cast from 'const unsigned char *const *' to 'const char *const *' is not allowedVC++ cannot convert argument from 'CString' to 'const char *'Correct use of PlaySound function in C++Efficient way to pass expressions of local variables to third party library (Ipopt)Cannot initialize const int from unpacked tupleWriting to an Output FileFunction receiving a const struct and cin commend
Why is consensus so controversial in Britain?
How can saying a song's name be a copyright violation?
Neighboring nodes in the network
What to put in ESTA if staying in US for a few days before going on to Canada
Does a druid starting with a bow start with no arrows?
How to take photos in burst mode, without vibration?
Where does SFDX store details about scratch orgs?
What's the point of deactivating Num Lock on login screens?
What do you call someone who asks many questions?
What does it mean to describe someone as a butt steak?
In a Spin are Both Wings Stalled?
Arrow those variables!
Is "remove commented out code" correct English?
Watching something be written to a file live with tail
Twin primes whose sum is a cube
Stopping power of mountain vs road bike
Forgetting the musical notes while performing in concert
Were any external disk drives stacked vertically?
If human space travel is limited by the G force vulnerability, is there a way to counter G forces?
Would Slavery Reparations be considered Bills of Attainder and hence Illegal?
Anagram holiday
I Accidentally Deleted a Stock Terminal Theme
Blender 2.8 I can't see vertices, edges or faces in edit mode
Could gravitational lensing be used to protect a spaceship from a laser?
Connect two third party modules with “const char*” and “char*” arguments
How to convert a std::string to const char* or char*?Convert const char* type to int arrayWhat is the difference between using Str.c_str() directly and use it to initialize a const char *?static_cast from 'const unsigned char *const *' to 'const char *const *' is not allowedVC++ cannot convert argument from 'CString' to 'const char *'Correct use of PlaySound function in C++Efficient way to pass expressions of local variables to third party library (Ipopt)Cannot initialize const int from unpacked tupleWriting to an Output FileFunction receiving a const struct and cin commend
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have two third party modules and I have to combine them.
First I get data from a class. I will submit this data to a function.
bool loadLibrary(const char *strPlugName)
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
return false;
The const char * strPlugName
is a value that I got from another library. I cannot change this value type myself.
Inside the function I try to call a BASS Library function.
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
Definition: typedef HPLUGIN (*BASS_PluginLoad_Type)(char *file,DWORD flags);
Here Xcode tell me:
Cannot initialize a parameter of type 'char *' with an rvalue of type 'const char *'
My question is how I can convert or cast this const char *
to char *
?
c++
New contributor
add a comment |
I have two third party modules and I have to combine them.
First I get data from a class. I will submit this data to a function.
bool loadLibrary(const char *strPlugName)
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
return false;
The const char * strPlugName
is a value that I got from another library. I cannot change this value type myself.
Inside the function I try to call a BASS Library function.
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
Definition: typedef HPLUGIN (*BASS_PluginLoad_Type)(char *file,DWORD flags);
Here Xcode tell me:
Cannot initialize a parameter of type 'char *' with an rvalue of type 'const char *'
My question is how I can convert or cast this const char *
to char *
?
c++
New contributor
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago
add a comment |
I have two third party modules and I have to combine them.
First I get data from a class. I will submit this data to a function.
bool loadLibrary(const char *strPlugName)
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
return false;
The const char * strPlugName
is a value that I got from another library. I cannot change this value type myself.
Inside the function I try to call a BASS Library function.
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
Definition: typedef HPLUGIN (*BASS_PluginLoad_Type)(char *file,DWORD flags);
Here Xcode tell me:
Cannot initialize a parameter of type 'char *' with an rvalue of type 'const char *'
My question is how I can convert or cast this const char *
to char *
?
c++
New contributor
I have two third party modules and I have to combine them.
First I get data from a class. I will submit this data to a function.
bool loadLibrary(const char *strPlugName)
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
return false;
The const char * strPlugName
is a value that I got from another library. I cannot change this value type myself.
Inside the function I try to call a BASS Library function.
HPLUGIN temp = _BASS_PluginLoad(strPlugName,0);
Definition: typedef HPLUGIN (*BASS_PluginLoad_Type)(char *file,DWORD flags);
Here Xcode tell me:
Cannot initialize a parameter of type 'char *' with an rvalue of type 'const char *'
My question is how I can convert or cast this const char *
to char *
?
c++
c++
New contributor
New contributor
edited 2 days ago
StoryTeller
104k13219282
104k13219282
New contributor
asked 2 days ago
Thomas DThomas D
445
445
New contributor
New contributor
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago
add a comment |
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
If and only if the function called via _BASS_PluginLoad
doesn't alter the memory pointed at by file
, you can use a const_cast
:
HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);
Some old c API's are not const correct on account of the const
keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast
is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
add a comment |
The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.
bool loadLibrary(const char *strPlugName)
std::string local(strPlugName);
local.push_back(''); // Ensure null terminated, if not using C++11 or greater
HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
return false;
If using C++17, you can just call local.data()
instead of &local[0]
.
Language lawyer caveat:
Strictly speaking, &local[0]
was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correctlocal.data()
however.
– StoryTeller
2 days ago
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
2 days ago
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Thomas D is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55468898%2fconnect-two-third-party-modules-with-const-char-and-char-arguments%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If and only if the function called via _BASS_PluginLoad
doesn't alter the memory pointed at by file
, you can use a const_cast
:
HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);
Some old c API's are not const correct on account of the const
keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast
is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
add a comment |
If and only if the function called via _BASS_PluginLoad
doesn't alter the memory pointed at by file
, you can use a const_cast
:
HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);
Some old c API's are not const correct on account of the const
keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast
is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
add a comment |
If and only if the function called via _BASS_PluginLoad
doesn't alter the memory pointed at by file
, you can use a const_cast
:
HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);
Some old c API's are not const correct on account of the const
keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast
is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.
If and only if the function called via _BASS_PluginLoad
doesn't alter the memory pointed at by file
, you can use a const_cast
:
HPLUGIN temp = _BASS_PluginLoad(const_cast<char*>(strPlugName),0);
Some old c API's are not const correct on account of the const
keyword being a fairly late addition to the C language. But they still don't mutate their arguments, so a const_cast
is the easiest way to make use of them in const correct C++ wrappers. It's a perfectly legitimate reason (maybe even the reason) for that cast.
edited 2 days ago
answered 2 days ago
StoryTellerStoryTeller
104k13219282
104k13219282
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
add a comment |
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
1
1
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
Note that only some APIs can be fixed like this. Others actually do temporarily mutate their argument. (There's a notorious Win32 API which does this, but I can't remember which it is.)
– Martin Bonner
2 days ago
add a comment |
The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.
bool loadLibrary(const char *strPlugName)
std::string local(strPlugName);
local.push_back(''); // Ensure null terminated, if not using C++11 or greater
HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
return false;
If using C++17, you can just call local.data()
instead of &local[0]
.
Language lawyer caveat:
Strictly speaking, &local[0]
was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correctlocal.data()
however.
– StoryTeller
2 days ago
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
2 days ago
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
2 days ago
add a comment |
The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.
bool loadLibrary(const char *strPlugName)
std::string local(strPlugName);
local.push_back(''); // Ensure null terminated, if not using C++11 or greater
HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
return false;
If using C++17, you can just call local.data()
instead of &local[0]
.
Language lawyer caveat:
Strictly speaking, &local[0]
was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correctlocal.data()
however.
– StoryTeller
2 days ago
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
2 days ago
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
2 days ago
add a comment |
The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.
bool loadLibrary(const char *strPlugName)
std::string local(strPlugName);
local.push_back(''); // Ensure null terminated, if not using C++11 or greater
HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
return false;
If using C++17, you can just call local.data()
instead of &local[0]
.
Language lawyer caveat:
Strictly speaking, &local[0]
was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).
The easy and safe way is to copy the argument into a local buffer, and then pass a pointer to that. As you are using C++, you can automate the memory management.
bool loadLibrary(const char *strPlugName)
std::string local(strPlugName);
local.push_back(''); // Ensure null terminated, if not using C++11 or greater
HPLUGIN temp = _BASS_PluginLoad(&local[0],0);
return false;
If using C++17, you can just call local.data()
instead of &local[0]
.
Language lawyer caveat:
Strictly speaking, &local[0]
was not defined to work in C++98 - in practice it always did (and later versions of the standard defined it to work).
edited 2 days ago
Toby Speight
17.4k134368
17.4k134368
answered 2 days ago
Martin BonnerMartin Bonner
23.8k33267
23.8k33267
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correctlocal.data()
however.
– StoryTeller
2 days ago
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
2 days ago
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
2 days ago
add a comment |
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correctlocal.data()
however.
– StoryTeller
2 days ago
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
2 days ago
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
2 days ago
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correct
local.data()
however.– StoryTeller
2 days ago
You can skip the null termination since C++11 if memory serves. You do need C++17 for the const correct
local.data()
however.– StoryTeller
2 days ago
@StoryTeller
local.data()
is null-terminated in C++11, but I'm not sure &local[0]
is - BICBW.– Martin Bonner
2 days ago
@StoryTeller
local.data()
is null-terminated in C++11, but I'm not sure &local[0]
is - BICBW.– Martin Bonner
2 days ago
They both produce the same buffer (as does
c_str
) since C++11. The null terminator is guaranteed.– StoryTeller
2 days ago
They both produce the same buffer (as does
c_str
) since C++11. The null terminator is guaranteed.– StoryTeller
2 days ago
add a comment |
Thomas D is a new contributor. Be nice, and check out our Code of Conduct.
Thomas D is a new contributor. Be nice, and check out our Code of Conduct.
Thomas D is a new contributor. Be nice, and check out our Code of Conduct.
Thomas D is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55468898%2fconnect-two-third-party-modules-with-const-char-and-char-arguments%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Not to get crucified, but const_cast ... ouch...
– user1810087
2 days ago