¿Cómo puedo mover el puntero de una clase a otra en c ++?

I have class named CiF and I want to move in the constructor pointer of CiF to new class named Cmanager. I tried something like this:

CiF::CiF() : Cmanager(new Cmanager(CiF THIS))
{
}

please help thanks

preguntado el 08 de enero de 11 a las 17:01

Perhaps you can show some code (or at least some pseudocode) that demonstrates what you'd like to accomplish. -

2 Respuestas

The following code shows how a pointer of one class is given to another in ctor. I think thats what you're trying to achieve.

I HIGHLY recommend you on not using code like this, but you did not give enough information about your usecase and so I'm not able to give you alternatives.

You probably should take a look into smart pointers and general class design. (When giving pointers to any other class you need to control which class owns the object, deletes it etc)

class CiF;

class Cmanager
{
public:
    Cmanager() :
      cif_(0)
    {};
    void setCiF(CiF* cif)
    {
        cif_ = cif;
    }
private:
    CiF* cif_;
};

class CiF
{
public:
    CiF() :
      cmanager_(Cmanager())
    {
        cmanager_.setCiF(this);
    }

private:
    Cmanager cmanager_;
};

Respondido el 17 de enero de 11 a las 18:01

En el caso anterior, class CiF should be forward declared. - Mahesh

With out having a relationship between CiF y Cmanager, I think it cannot be accomplished.

#include <iostream>
class Cmanager
{
};

class Cif:public Cmanager
{
    Cmanager *ptr ;
    public:
    bar() : ptr(this) 
    {
    } ;
};

int main()
{
    Cif obj ;
    return 0;
}

Respondido el 08 de enero de 11 a las 21:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.