在下面的代码中我不想使用 reinterpret_cast 任何人都可以建议我一个好的解决方案而不是 reinterpret_cast 吗?

问题描述 投票:0回答:2

在下面的代码中我不想使用 reinterpret_cast 任何人都可以建议我一个好的解决方案而不是 reinterpret_cast 吗??

#include<iostream>
#include<memory>
using namespace std;

//New class which i created
class Commonline
{
 public:
 int Newkey1,Newkey2,Newkey3;
 
 Commonline(){Newkey1=0;Newkey2=0;Newkey3=0; cout<<"Commonline default constructor\n";}

 void Commoninit()
 {
     Newkey1=10;Newkey2=20;Newkey3=30;
 }
 int get_Newkey1()const{return Newkey1;}
 int get_Newkey2()const{return Newkey2;}
 int get_Newkey3()const{return Newkey3;}
 
 ~Commonline(){cout<<"Commonline distructor\n";}
};

//already existing old class
class straightline
{
 public:
 int Oldkey1,Oldkey2,Oldkey3;
 
 straightline(){Oldkey1=0;Oldkey2=0;Oldkey3=0;cout<<"straightline default constructor\n";}

//Here in this old class have created this new constructor for filling new class var values with 
//this old class variables so that to match return type of execute_this().
 straightline(const Commonline *ptr)
 {
    cout<<"straightline parameterised constructor\n";
    Oldkey1 = ptr->get_Newkey1();
    Oldkey2 = ptr->get_Newkey2();
    Oldkey3 = ptr->get_Newkey3();
 }
 void display_strData()
 {
     cout<<"Oldkey1 = "<<Oldkey1<<endl;
     cout<<"Oldkey2 = "<<Oldkey2<<endl;
     cout<<"Oldkey3 = "<<Oldkey3<<endl;
 }
 ~straightline()
 {
     cout<<"straightline distructor\n";
 }

};

//Already existing old function which excpects return type must be straightline*
straightline* execute_this()
{
 //here i wanted to return straightline ptr which holds Commonline object.
 Commonline* Comptr = new Commonline; 
 Comptr->Commoninit();

 straightline *strptr = reinterpret_cast<straightline*>(Comptr);//Please suggest any other good method
 if(strptr == nullptr)
 {
     cout<<"returned null pointer\n";
     return nullptr;
 }
 return strptr;
}

int main()
{
 straightline *strptr = execute_this();
 strptr->display_strData();
}

我知道在这里使用 reinterpret_cast 将是一个坏主意,因为将来如果成员变量的任何顺序发生变化,整个对象都会受到干扰,只是为了理解我的问题/要求我已经使用了它。 任何人都可以建议我一个解决方案而不是 reinterpret_cast 吗?

注意: 我尝试使用 unique_ptr.reset() 代替 reinterpret_cast<> 如下

  Commonline* Comptr = new Commonline;
  static std::unique_ptr<straightline> strptr;
  strptr.reset(new straightline(Comptr));

  return strptr.get();

但是在合并更改后它会崩溃,因为如果多个进程调用 execute_this() 然后重置似乎会删除旧的 ptr。

c++ class pointers return reinterpret-cast
2个回答
0
投票

如果你想要一个

straightline
对象那么你必须构造一个

if (Comptr == nullptr)
{
    cout<<"returned null pointer\n";
    return nullptr;
}
return new straightline(*Comptr);

为此,您需要向

straightline
添加一个构造函数,它可以从
straightline
.
 生成 
Commonline

class straightline
{
public:
    int Oldkey1,Oldkey2,Oldkey3;
 
    straightline()
    {
        Oldkey1=0;
        Oldkey2=0;
        Oldkey3=0;
        cout<<"straightline default constructor\n";
    }

    straightline(const Commonline& c)
    {
        Oldkey1=c.get_Newkey1();
        Oldkey2=c.get_Newkey2();
        Oldkey3=c.get_Newkey3();
        cout<<"straightline Commonline constructor\n";
    }

    ...
};

现在您的代码还有其他问题(内存泄漏等),但这是解决您询问的问题的基本思路。


0
投票

真的不需要init方法。只需实现 init 所做的三参数构造函数。那么你就不需要

execute_this
方法了。 虽然如果你真的想在
execute_this
方法中实现一些逻辑,你可以做这样的事情:

template<typename T>
T* execute_this(T* arg) {
//do some logic with arg
return arg;//or whatever you like
}

或者您可以从

Commonline
派生出
straightline
并使用它的基指针。

© www.soinside.com 2019 - 2024. All rights reserved.