468,719 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

member variable declaration and forward declarations

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
Aug 26 '05 #1
2 2100
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
Aug 26 '05 #2
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
Aug 26 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

83 posts views Thread by Alexander Zatvornitskiy | last post: by
134 posts views Thread by James A. Donald | last post: by
2 posts views Thread by Mark Sisson | last post: by
2 posts views Thread by xllx.relient.xllx | last post: by
11 posts views Thread by Jef Driesen | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Oskars | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.