Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > struct declaration into another struct

Reply
Thread Tools

struct declaration into another struct

 
 
slocum
Guest
Posts: n/a
 
      04-11-2008
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...
 
Reply With Quote
 
 
 
 
Looney
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 3:06 pm, slocum <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      04-11-2008
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
slocum
Guest
Posts: n/a
 
      04-11-2008
On 11 Kwi, 08:42, "Jim Langston" <(E-Mail Removed)> 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
> (E-Mail Removed)


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 ????
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
assign a struct variable into another struct variable ineedyourluvin1@yahoo.com C++ 18 07-22-2005 04:08 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM



Advertisments