Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Really, DarkGDK? You don't like std::strings?

Author
Message
Qqite
14
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 21st Jan 2013 11:03 Edited at: 21st Jan 2013 11:05
Why on earth does dbLoadObject not take const char*...

Maybe I'm looking past something but dbLoadObject doesn't take const char* as a parameter, only char*. And whenever I try to do so, it can't cast (for good reason).

This is easily solved if I type in dbLoadObject("Someobject.obj",1) or something, but if I do this

Then I get an obvious cast error of


How would I get about this?
I've tried

But that's not allowed. :\

Ventures of the worlds around us are limited only by our imagination.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 21st Jan 2013 12:19
Just do a typecast.



"everyone forgets a semi-colon sometimes." - Phaelax
Qqite
14
Years of Service
User Offline
Joined: 28th Feb 2010
Location: Florida, U.S.
Posted: 21st Jan 2013 12:55
gdgdgwegherh

I was trying to do a static cast and everything but I didn't do the simplest one. Silly :\

Ventures of the worlds around us are limited only by our imagination.
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 21st Jan 2013 17:22
It would be better to memcpy it over to a char pointer, or even to use a const-cast rather than a C-style cast.

Of course, that's programmer preference so you can ignore me.

Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 22nd Jan 2013 06:15
Quote: "It would be better to memcpy it over to a char pointer"


Not really. The data inside both blocks of memory will be exactly the same, and you're using up time copying over (potentially large) strings. Not to mention extra time allocating and freeing the string, all just for one function call.

"everyone forgets a semi-colon sometimes." - Phaelax
Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 22nd Jan 2013 14:47
Quote: " Not really. The data inside both blocks of memory will be exactly the same, and you're using up time copying over (potentially large) strings. Not to mention extra time allocating and freeing the string, all just for one function call."

That's true, but if you do copy it over then you don't have to worry about the function editing the std::string's contents either accidentally or due to some error.

And my other point still stands, the C++ const-cast is better than the C-style cast in this instance(explicitly tells readers of your code that you're taking away the constness of the returned const char pointer).

Login to post a reply

Server time is: 2024-04-19 11:32:21
Your offset time is: 2024-04-19 11:32:21