Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   struct declaration into another struct (http://www.velocityreviews.com/forums/t609966-struct-declaration-into-another-struct.html)

slocum 04-11-2008 05:06 AM

struct declaration into another struct
 
I have a struct like that

struct A {
struct B {
.....
}
...
}

I have to declare previously all that struct and their friends

struct A;
friend struct A;

but how to declare struct B and which is declared into struct A ?

struct A::B; ?
friend struct A::B ?

it doesn't work...

Looney 04-11-2008 06:33 AM

Re: struct declaration into another struct
 
On Apr 11, 3:06 pm, slocum <piotr270...@interia.pl> wrote:
> I have a struct like that
>
> struct A {
> struct B {
> .....
> }
> ...
>
> }
>
> I have to declare previously all that struct and their friends
>
> struct A;
> friend struct A;
>
> but how to declare struct B and which is declared into struct A ?
>
> struct A::B; ?
> friend struct A::B ?
>
> it doesn't work...


You can not just forward declare A::B and make it work
a nested type must be visible before hand so consider doing the
following
class D
{
friend struct A;
};

struct A
{
int m_i;
struct B
{
int m_i;
};
};

class C
{
friend struct A;
friend struct A::B;
};

void somefunc()
{
D d;
C c;
}

make sure A and B are defined in the translation unit,
though usually a class type(class, structor union) does
not need to be declared or defined before a friend declaration
though if it is a class template (say in my eg if C was class template
then)
then A and B must be visible before hand(at least fwd declared).

It seems like the same is required for nested class types as well.

Also watch out for the following things are

Jim Langston 04-11-2008 06:42 AM

Re: struct declaration into another struct
 
slocum wrote:
> I have a struct like that
>
> struct A {
> struct B {
> .....
> }
> ...
> }
>
> I have to declare previously all that struct and their friends
>
> struct A;
> friend struct A;
>
> but how to declare struct B and which is declared into struct A ?
>
> struct A::B; ?
> friend struct A::B ?
>
> it doesn't work...


What doesn't work? The following compiles for me:

struct A
{
friend struct B;
struct B
{
int x;
};
int y;
};

struct C
{
friend struct A::B;
};

int main()
{

}

Does this answer your question?


--
Jim Langston
tazmaster@rocketmail.com



slocum 04-11-2008 09:42 AM

Re: struct declaration into another struct
 
On 11 Kwi, 08:42, "Jim Langston" <tazmas...@rocketmail.com> wrote:
> slocum wrote:
> > I have a struct like that

>
> > struct A {
> > struct B {
> > .....
> > }
> > ...
> > }

>
> > I have to declare previously all that struct and their friends

>
> > struct A;
> > friend struct A;

>
> > but how to declare struct B and which is declared into struct A ?

>
> > struct A::B; ?
> > friend struct A::B ?

>
> > it doesn't work...

>
> What doesn't work? The following compiles for me:
>
> struct A
> {
> friend struct B;
> struct B
> {
> int x;
> };
> int y;
>
> };
>
> struct C
> {
> friend struct A::B;
>
> };
>
> int main()
> {
>
> }
>
> Does this answer your question?
>
> --
> Jim Langston
> tazmas...@rocketmail.com


and what if struct A and B are template ? How to declare a friend for
nested template struct ?
template <class> friend struct A::B or what ????


All times are GMT. The time now is 03:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.