how to create a member, which knows about the object it was created in

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • di boruman

    how to create a member, which knows about the object it was created in

    Hi,

    I would like to let a member a of class A know about the object it
    was created in:

    A a(this);

    'this' would be stored in a member of A, for example

    B* b;

    for this purpose I included A.h in B.h and B.h in A.h (without
    problems) but when I try to declare the 'B* b;' I get a strange
    error (error C2501 - the error message is in Japanese
    (I use the Japanese version of Visual C++ .NET) and
    says something like "even if the identifier was declared,
    his shape is unspecified).

    here is the miminum code producing the error:

    int main(int argc, char* argv[])
    {
    return 0;
    }

    class A
    {
    public:
    B* b; // if I comment this line out everything works fine
    A(void) {};
    ~A(void) {};
    };

    class B
    {
    public:
    // A* a;
    B(void) {};
    ~B(void) {};
    };

    Actually I would like to also let B know about A, see the comment in
    class B...

    Is there anybody who can tell me, why this is not possible??
    Or even better how it could be possible???
    (or why it makes sense to forbid things like this - for the case
    it doesn't make sense...)

    any help??
    Thanks a lot :), di

    PS: The context of this problem is the following:
    I try to implement a little Genetic Algorithm and have a
    class 'Individual' with two members: 'Chromosome' and 'Phenotype'.
    I want the Phenotype to be able to adress the corresponding
    cromosome. As the individual knows about the
    chromosome, the easyest way would be to let the phenotype know
    about the individual it is belonging to....
  • John Harrison

    #2
    Re: how to create a member, which knows about the object it was created in


    "di boruman" <diboru@freenet .de> wrote in message
    news:47f05fa9.0 308140558.24fa4 565@posting.goo gle.com...[color=blue]
    > Hi,
    >
    > I would like to let a member a of class A know about the object it
    > was created in:
    >
    > A a(this);
    >
    > 'this' would be stored in a member of A, for example
    >
    > B* b;
    >
    > for this purpose I included A.h in B.h and B.h in A.h (without
    > problems) but when I try to declare the 'B* b;' I get a strange
    > error (error C2501 - the error message is in Japanese
    > (I use the Japanese version of Visual C++ .NET) and
    > says something like "even if the identifier was declared,
    > his shape is unspecified).
    >
    > here is the miminum code producing the error:
    >
    > int main(int argc, char* argv[])
    > {
    > return 0;
    > }
    >[/color]

    Add this here, its called a forward declaration.

    class B;
    [color=blue]
    > class A
    > {
    > public:
    > B* b; // if I comment this line out everything works fine
    > A(void) {};
    > ~A(void) {};
    > };
    >
    > class B
    > {
    > public:
    > // A* a;
    > B(void) {};
    > ~B(void) {};
    > };
    >
    > Actually I would like to also let B know about A, see the comment in
    > class B...
    >
    > Is there anybody who can tell me, why this is not possible??
    > Or even better how it could be possible???
    > (or why it makes sense to forbid things like this - for the case
    > it doesn't make sense...)
    >
    > any help??
    > Thanks a lot :), di
    >[/color]

    john


    Comment

    Working...