Connect two third party modules with “const char*” and “char*” arguments
.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++
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++
Not to get crucified, but const_cast ... ouch...
– user1810087
Apr 2 at 7:16
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++
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++
edited Apr 2 at 7:30
StoryTeller
106k13222285
106k13222285
asked Apr 2 at 7:12
Thomas DThomas D
585
585
Not to get crucified, but const_cast ... ouch...
– user1810087
Apr 2 at 7:16
add a comment |
Not to get crucified, but const_cast ... ouch...
– user1810087
Apr 2 at 7:16
Not to get crucified, but const_cast ... ouch...
– user1810087
Apr 2 at 7:16
Not to get crucified, but const_cast ... ouch...
– user1810087
Apr 2 at 7:16
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
Apr 2 at 7:28
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
Apr 2 at 7:29
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
Apr 2 at 7:33
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
});
}
});
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
Apr 2 at 7:28
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
Apr 2 at 7:28
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 Apr 2 at 7:36
answered Apr 2 at 7:16
StoryTellerStoryTeller
106k13222285
106k13222285
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
Apr 2 at 7:28
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
Apr 2 at 7:28
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
Apr 2 at 7:28
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
Apr 2 at 7:28
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
Apr 2 at 7:29
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
Apr 2 at 7:33
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
Apr 2 at 7:29
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
Apr 2 at 7:33
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 Apr 2 at 13:54
Toby Speight
17.6k134469
17.6k134469
answered Apr 2 at 7:26
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
Apr 2 at 7:29
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
Apr 2 at 7:33
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
Apr 2 at 7:29
@StoryTellerlocal.data()
is null-terminated in C++11, but I'm not sure&local[0]
is - BICBW.
– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as doesc_str
) since C++11. The null terminator is guaranteed.
– StoryTeller
Apr 2 at 7:33
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
Apr 2 at 7:29
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
Apr 2 at 7:29
@StoryTeller
local.data()
is null-terminated in C++11, but I'm not sure &local[0]
is - BICBW.– Martin Bonner
Apr 2 at 7:32
@StoryTeller
local.data()
is null-terminated in C++11, but I'm not sure &local[0]
is - BICBW.– Martin Bonner
Apr 2 at 7:32
They both produce the same buffer (as does
c_str
) since C++11. The null terminator is guaranteed.– StoryTeller
Apr 2 at 7:33
They both produce the same buffer (as does
c_str
) since C++11. The null terminator is guaranteed.– StoryTeller
Apr 2 at 7:33
add a comment |
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
Apr 2 at 7:16