|
-
November 15th, 2010, 10:02 AM
#1
Class containing struct referencing class
For this problem, it might be simpler to begin with a code extract:
Code:
class Pattern
{
public:
struct ChildPattern
{
Pattern pattern;
int offset;
};
};
Instances of "ChildPattern" are going to be kept in lists and the like, and "offset" is something associated with instances of Pattern but it is not intrinsic to them (i.e. it shouldn't be part of the class, except as a hack).
Anyway the problem is that the above code doesn't compile. I get
Pattern::ChildPattern:: pattern uses undefined class 'Pattern'
.
Any suggestions? I don't want to change the first struct element to "Pattern * pPattern" if I can help it: the class is simple and I don't want to add new() and delete() unnecessarily.
-
November 15th, 2010, 10:12 AM
#2
Re: Class containing struct referencing class
If this is in a header, you could write:
Code:
class Pattern
{
public:
};
namespace detail
{
struct ChildPattern
{
Pattern pattern;
int offset;
};
}
with the convention that whatever is within a namespace named detail is implementation detail and hence private by convention.
EDIT:
Oh wait, I noticed that the nested class is public. Therefore, it would suffice to make it non-nested without the detail namespace (i.e., keeping it in the same namespace as Pattern will do).
-
November 15th, 2010, 12:44 PM
#3
Re: Class containing struct referencing class
Thanks. Still having trouble though.
I should have added that the class needs to use this structure too (shown below), so evidently to the compiler, it is a circular definition.
The thing is though, to my brain, it isn't a circular definition ( ), and there must be a way to push this through. I can only think of hack ways at the mo.
Code:
class Pattern
{
public:
struct ChildPattern
{
Pattern pattern;
int offset;
};
void AddChild (ChildPattern & child);
};
-
November 15th, 2010, 12:50 PM
#4
Re: Class containing struct referencing class
You can use a forward declaration, e.g.,
Code:
struct ChildPattern;
class Pattern
{
public:
void AddChild(ChildPattern& child);
};
struct ChildPattern
{
Pattern pattern;
int offset;
};
-
November 15th, 2010, 12:56 PM
#5
Re: Class containing struct referencing class
That sorted it.
I had tried forward declaring the class, but for whatever reason I had a mental blindspot to the idea of forward declaring the struct.
Thanks laserlight, I've just rated your post
-
November 15th, 2010, 09:06 PM
#6
Re: Class containing struct referencing class
Wait a minute...
It's fine if you two got it working, but the OP's original code snippet was a class that (more or less indirectly) contained a member of its own type, which, for obvious reasons after some second thought, is never, never, never allowed in C++. 
Or did I miss some obscure loophole here?
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 16th, 2010, 02:31 AM
#7
Re: Class containing struct referencing class
 Originally Posted by Eri523
Wait a minute...
It's fine if you two got it working, but the OP's original code snippet was a class that (more or less indirectly) contained a member of its own type, which, for obvious reasons after some second thought, is never, never, never allowed in C++.
Or did I miss some obscure loophole here? 
No, the (base) class defines a subclass that contains an instance of said base class. However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself. However, you are perfectly correct about the fact that it is not legal (or even possible) to have a member of yourself.
The OP's code did not fail because of this. THIS code though does...
Code:
class Pattern
{
public:
struct ChildPattern
{
Pattern pattern;
int offset;
} an_instance;
ChildPattern another_instance;
};
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 16th, 2010, 04:28 AM
#8
Re: Class containing struct referencing class
 Originally Posted by monarch_dodra
No, the (base) class defines a subclass that contains an instance of said base class. However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself.
I would rather say that the enclosing class definition contains a nested class definition that has an enclosing class object as a member. Base class, subclass and derived class usually imply that inheritance is involved, but that is not the case here.
-
November 16th, 2010, 07:28 AM
#9
Re: Class containing struct referencing class
 Originally Posted by monarch_dodra
However, the actual base class does not contain an instance of the derived class, and as such, does not contain an instance of itself.
Ah, I see. Thanks, now I'm content again. 
Am I right to assume that in this case the visibility of ChildPattern is limited to the scope of the Pattern declaration? I.e. the member function from post #3 could only be called from inside another member function?
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
November 16th, 2010, 08:31 AM
#10
Re: Class containing struct referencing class
 Originally Posted by Mijin
Code:
class Pattern
{
public:
struct ChildPattern
{
Pattern pattern;
int offset;
};
void AddChild (ChildPattern & child);
};
 Originally Posted by Eri523
Am I right to assume that in this case the visibility of ChildPattern is limited to the scope of the Pattern declaration? I.e. the member function from post #3 could only be called from inside another member function?
Am not sure I completely understand your question (especially since post #3 doesn't compile) but my answer would be no. Nesting a declaration merely ads scope to the declarations (like namespaces), and since everything here is public, this would be perfectly legal:
Code:
Pattern myPattern;
Pattern::ChildPattern myChild;
myPattern.AddChild(myChild);
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 16th, 2010, 08:36 AM
#11
Re: Class containing struct referencing class
Thanks, monarch_dodra. You understood my question perfectly and your answer was exactly what I wanted to know.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|