member variable declaration and forward declarations

J

John Ratliff

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
 
V

Victor Bazarov

John said:
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;

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
 
J

John Ratliff

Victor said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top