Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about struct keyword

Reply
Thread Tools

about struct keyword

 
 
lp-boy@yandex.ru
Guest
Posts: n/a
 
      12-07-2005
Hello!

Is the following code legal?

template<class T>
struct holder{};

struct A
{
holder<struct B*> h; //!!!
//but holder<B*> h; error: B was not declared
};

int main()
{
A a;
}

If yes, why? Thanks in advance.

 
Reply With Quote
 
 
 
 
Niklas Norrthon
Guest
Posts: n/a
 
      12-07-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

> Hello!
>
> Is the following code legal?
>
> template<class T>
> struct holder{};
>
> struct A
> {
> holder<struct B*> h; //!!!
> //but holder<B*> h; error: B was not declared
> };
>
> int main()
> {
> A a;
> }
>
> If yes, why? Thanks in advance.


In order to use a type, its size must be known. In case of pointers a
forward declaration is all that is needed, since pointer size does not
depend on the internals of the type. All pointers to user defined types
have the same size.

In your code you say holder<struct B*> h;
The struct word makes this a forward declaration for B at the latest
possible moment.
When you just say holder<B*> B isn't declared yet, so it can't be used.

/Niklas Norrthon

 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      12-07-2005
(E-Mail Removed) wrote:
> Hello!
>
> Is the following code legal?
>
> template<class T>
> struct holder{};
>
> struct A
> {
> holder<struct B*> h; //!!!
> //but holder<B*> h; error: B was not declared
> };
>
> int main()
> {
> A a;
> }
>
> If yes, why? Thanks in advance.


The way I interpret it, the line

holder<struct B*> h;

is equivalent to forward-declaring B as a struct.


template <class T>
struct holder { };

struct B;

struct A {
holder<B*> h;
};

int main()
{
A a;
}


works for me (VS .NET 2003).

--
Marcus Kwok
 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      12-07-2005

Marcus Kwok wrote:
> (E-Mail Removed) wrote:
> > Hello!
> >
> > Is the following code legal?
> >
> > template<class T>
> > struct holder{};
> >
> > struct A
> > {
> > holder<struct B*> h; //!!!
> > //but holder<B*> h; error: B was not declared
> > };
> >
> > int main()
> > {
> > A a;
> > }
> >
> > If yes, why? Thanks in advance.

>
> The way I interpret it, the line
>
> holder<struct B*> h;
>
> is equivalent to forward-declaring B as a struct.
>
>
> template <class T>
> struct holder { };
>
> struct B;
>
> struct A {
> holder<B*> h;
> };
>
> int main()
> {
> A a;
> }


The two programs are similar but not the same. The struct B declaration
appears first within struct "A" therefore it is a forward declaration
for an inner struct of A, named B (or ::A::B fully qualified). Of
course there are no inner structs defined in struct A, let alone one
named B.

But no matter, since the holder class template is being instantiated
with a pointer to B, the fact that there is no such type is not an
error.

Greg

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      12-07-2005
Greg <(E-Mail Removed)> wrote:
> Marcus Kwok wrote:
>> (E-Mail Removed) wrote:
>> > Hello!
>> >
>> > Is the following code legal?
>> >
>> > template<class T>
>> > struct holder{};
>> >
>> > struct A
>> > {
>> > holder<struct B*> h; //!!!
>> > //but holder<B*> h; error: B was not declared
>> > };
>> >
>> > int main()
>> > {
>> > A a;
>> > }
>> >
>> > If yes, why? Thanks in advance.

>>
>> The way I interpret it, the line
>>
>> holder<struct B*> h;
>>
>> is equivalent to forward-declaring B as a struct.
>>
>>
>> template <class T>
>> struct holder { };
>>
>> struct B;
>>
>> struct A {
>> holder<B*> h;
>> };
>>
>> int main()
>> {
>> A a;
>> }

>
> The two programs are similar but not the same. The struct B declaration
> appears first within struct "A" therefore it is a forward declaration
> for an inner struct of A, named B (or ::A::B fully qualified). Of
> course there are no inner structs defined in struct A, let alone one
> named B.


OK, then what I really meant <g> was:

template <class T>
struct holder { };

struct A {
struct B;
holder<B*> h;
};

int main()
{
A a;
}

--
Marcus Kwok
 
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
Why do I have to prefix stat from <sys/stat.h> with the keyword struct? Rolf Krüger C++ 2 03-12-2008 10:38 PM
RE: keyword checker - keyword.kwlist Hamilton, William Python 4 05-13-2007 06:31 AM
keyword checker - keyword.kwlist tom@finland.com Python 6 05-10-2007 04:53 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