Comments (7)
Hi, @Jtaim Can you paste complete codes?
from cpp-primer.
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.
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.
i see were i went wrong now. used static_cast instead of const_cast.
well i'm still new at this. :)
from cpp-primer.
@Jtaim It doesn't matter.
from cpp-primer.
Hi guys~
If no further discussion needed, please remember to close this issue.
@Jtaim @pezy
from cpp-primer.
thx @Jtaim
from cpp-primer.
Related Issues (20)
- exercise12.8 memory leakage reason?
- Got this issue
- Typo in exercise 15.34 / 15.36
- Typo in exercise 16.36
- Cpp primer answer
- Access to private member
- Cannot understand exercise 9.52, please help. HOT 1
- ex13.44 about size and length function HOT 1
- D HOT 1
- ex2.5 notation vs type? HOT 1
- Spelling error in "Exercise 4.7" HOT 1
- Spelling error in Exercise 4.14 HOT 1
- Spelling error in Exercise 4.28 HOT 1
- "Include" error in ex15.4.5.6
- Cpp
- Ch 16 - some further reading for ex65 and 66
- A issue made by mistake.
- Ex 14.28 problem
- Ex13.42 HOT 1
- Exercise 10_32.cc (G++ giving error on the accumulate algorithm)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp-primer.