member variable declaration and forward declarations

Discussion in 'C++' started by John Ratliff, Aug 26, 2005.

  1. John Ratliff

    John Ratliff Guest

    I'm having issues with forward declarations and possibly member variables.

    Can you declare a member variable and pass it parameters.

    class x {
    private:
    y obj(this);
    }

    Is that valid? I'm getting a lot of problems, but it may not be due to
    that. It may be due to the fact that the real X and Y require knowledge
    of each other.

    Here is a little example illustrating my problem. It would be a better
    example if we put foo and bar in different files and included their
    headers prior to the class def.

    #include <iostream>

    namespace myNS {
    class bar;

    class foo {
    private:
    bar b(this);

    public:
    void method() { std::cout << "foo"; }
    void do() { b.method(); }
    };

    class bar {
    private:
    foo &f;

    public:
    bar(foo &f) : f(f) {}
    void method() { f.method(); }
    };
    }

    int main(int, char **) {
    myNS::foo().do();

    return 0;
    }

    This is clearly no good. Is it possible for foo to create a bar instance
    b passing itself as a parameter?

    Thanks,

    --John Ratliff
    John Ratliff, Aug 26, 2005
    #1
    1. Advertising

  2. John Ratliff wrote:
    > I'm having issues with forward declarations and possibly member variables.
    >
    > Can you declare a member variable and pass it parameters.
    >
    > class x {
    > private:
    > y obj(this);
    > }
    >
    > Is that valid?


    No. What would that do? Initialise it? Then it wouldn't be
    a declaration. Initialisation of data members is done in a constructor
    initialiser list.

    > I'm getting a lot of problems, but it may not be due to
    > that. It may be due to the fact that the real X and Y require knowledge
    > of each other.
    >
    > Here is a little example illustrating my problem. It would be a better
    > example if we put foo and bar in different files and included their
    > headers prior to the class def.
    >
    > #include <iostream>
    >
    > namespace myNS {
    > class bar;
    >
    > class foo {
    > private:
    > bar b(this);


    That won't fly. If 'bar' is unknown, you can't declare a variable of
    that type. You need to move 'bar' class definition above 'foo', and
    then change the declaration to read

    bar b;

    >
    > public:


    You need to add a constructor declaration/definition here

    foo() : b(*this) {}

    > void method() { std::cout << "foo"; }
    > void do() { b.method(); }
    > };
    >
    > class bar {
    > private:
    > foo &f;
    >
    > public:
    > bar(foo &f) : f(f) {}
    > void method() { f.method(); }
    > };


    As I said before, you need to move the 'bar' class definition up, above
    the 'foo', and forward-declare 'class foo'.

    Then you need to _declare_ 'method' and define it _after_ 'foo' here:

    void bar::method() { f.method(); }

    > }
    >
    > int main(int, char **) {
    > myNS::foo().do();
    >
    > return 0;
    > }
    >
    > This is clearly no good. Is it possible for foo to create a bar instance
    > b passing itself as a parameter?


    Yes.

    V
    Victor Bazarov, Aug 26, 2005
    #2
    1. Advertising

  3. John Ratliff

    John Ratliff Guest

    Victor Bazarov wrote:
    > John Ratliff wrote:
    >
    >> I'm having issues with forward declarations and possibly member
    >> variables.
    >>
    >> Can you declare a member variable and pass it parameters.
    >>
    >> class x {
    >> private:
    >> y obj(this);
    >> }
    >>
    >> Is that valid?

    >
    >
    > No. What would that do? Initialise it? Then it wouldn't be
    > a declaration. Initialisation of data members is done in a constructor
    > initialiser list.
    >


    I can't believe I forgot about initializer lists...

    Thanks,

    --John Ratliff
    John Ratliff, Aug 26, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    1
    Views:
    586
    Jonathan Turkanis
    Feb 15, 2004
  2. Replies:
    3
    Views:
    455
    Ron Natalie
    Aug 11, 2006
  3. Steven T. Hatton
    Replies:
    2
    Views:
    436
    Steven T. Hatton
    Nov 20, 2006
  4. Replies:
    6
    Views:
    517
  5. Jef Driesen
    Replies:
    11
    Views:
    907
Loading...

Share This Page