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
Quote:
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.
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).
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);
};
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;
};
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 :)
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++. :eek:
Or did I miss some obscure loophole here? :ehh:
Re: Class containing struct referencing class
Quote:
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++. :eek:
Or did I miss some obscure loophole here? :ehh:
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;
};
Re: Class containing struct referencing class
Quote:
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.
Re: Class containing struct referencing class
Quote:
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?
Re: Class containing struct referencing class
Quote:
Originally Posted by
Mijin
Code:
class Pattern
{
public:
struct ChildPattern
{
Pattern pattern;
int offset;
};
void AddChild (ChildPattern & child);
};
Quote:
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);
Re: Class containing struct referencing class
Thanks, monarch_dodra. You understood my question perfectly and your answer was exactly what I wanted to know. :)