简体   繁体   中英

Conversion requires reinterpret_cast, C-style cast or function-style cast

Why do the compiler complain about casting here

 class DBconnection {
  public:
     DataSet* query(string qStr) {
     ConnImpl* conImpl = ConnectionPool::getInstance()->acquireConnection();
     DataSet *data = new DataSet();
     conImpl->doQuery(qStr,data);
     ConnectionPool::getInstance()->releaseConnection(conImpl);
     return data;
 }
};


 class Client {
  public:
     DataSet* queryDB(string q) {
     return new DBconnection()->query(q);
  }
 };

The Client::queryDB(std:::string) is actually returning a DataSet pointer through DBconnection::query(std::string).

The error:

error C2440: 'return' : cannot convert from 'DBconnection *' to 'DataSet *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

If I try for instance - c-casting, it doesnt work either

 return new (DataSet*)DBconnection()->query(q);

You've forgotten your operator precedence table:

return (new DBconnection())->query(q);

will work. new has a lower precedence than -> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM