Git Product home page Git Product logo

Comments (7)

pezy avatar pezy commented on May 26, 2024

Hi, @Jtaim Can you paste complete codes?

from cpp-primer.

pezy avatar pezy commented on May 26, 2024

static_cast from type 'const string* {aka const std::basic_string}' to type 'std::string

From the error message, I can see you want to convert const string * to string. But in exercise 4.37, pv is void*. Thus, following codes work fine:

string str("hello");
const string *ps = &str;
void *pv = const_cast<string*>(ps);
// ...

And, more exact solution is:

pv = static_cast<void*>(const_cast<string*>(ps));

So our answer:

pv = (void*)ps; // pv = const_cast<string*>(ps); or pv = static_cast<void*>(const_cast<string*>(ps));

from cpp-primer.

Jtaim avatar Jtaim commented on May 26, 2024

int main()
{
using std::string;
// rewrite each of the following old-style casts to use a named cast:
// pv = (void_)ps;
// i = int(_pc);
// pv = &d;
// pc = (char*)pv;

int i; double d; const string *ps; char *pc; void *pv;

pv = static_cast<string*>(ps);  // error
pv = static_cast<void*>(const_cast<string*>(ps));
i = static_cast<int>(*pc);
pv = static_cast<void*>(&d);
pc = reinterpret_cast<char*>(pv);
return 0;

}

from cpp-primer.

Jtaim avatar Jtaim commented on May 26, 2024

i see were i went wrong now. used static_cast instead of const_cast.
well i'm still new at this. :)

from cpp-primer.

pezy avatar pezy commented on May 26, 2024

@Jtaim It doesn't matter.

from cpp-primer.

Mooophy avatar Mooophy commented on May 26, 2024

Hi guys~
If no further discussion needed, please remember to close this issue.
@Jtaim @pezy

from cpp-primer.

Mooophy avatar Mooophy commented on May 26, 2024

thx @Jtaim

from cpp-primer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.