Q:

Rewrite each of the following old-style casts to use a named cast

0

Rewrite each of the following old-style casts to use a named cast:

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

(a) pv = (void*)ps;

(b) i = int(*pc);

(c) pv = &d;

(d) pc = (char*) pv;

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>

int main() {
  int i = 0;
  double d = 0;
  std::string str("some string");
  const std::string *ps = &str;
  char c = 'c';
  char *pc = &c;
  void *pv;

  //pv = (void*)ps;
  pv = static_cast<void *>(const_cast<std::string *>(ps));
  //pv = const_cast<std::string *>(ps);  // Also work.

  //i = int(*pc);
  i = static_cast<int>(*pc);

  //pv = &d;
  pv = static_cast<void *>(&d);

  //pc = (char*) pv;
  pc = static_cast<char *>(pv);

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now