473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

about struct keyword

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.

Dec 7 '05 #1
4 1501
lp****@yandex.ru 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
Dec 7 '05 #2

Marcus Kwok wrote:
lp****@yandex.ru 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

Dec 7 '05 #3
lp****@yandex.ru 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

Dec 7 '05 #4
Greg <gr****@pacbell.net> wrote:
Marcus Kwok wrote:
lp****@yandex.ru 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
Dec 7 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Kilana | last post by:
I see this all the time in code: typedef struct a_struct { ... }differentName, *differentNamePtr; I understand how I can use it, but could someone tell me why the above is
5
by: PCHOME | last post by:
Hello! I am working on dividing a single C file into several files. Now I encounter a problem about the global variables and can not find a way to solve it. All global variables and codes used...
20
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
4
by: Christoph Scholtes | last post by:
Hi, I have some questions about header files: Say I have a file functions.c which contains a couple of functions. I have declared some structs in this file too. The structs are defined in...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
9
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I...
6
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first...
7
by: ot_007_2001 | last post by:
Hi, all, For pricticing defination of structure in C, I wrote a small code as follow: #include<stdio.h> struct datastruct { int data;
6
by: Gestorm | last post by:
Hi everyone, I have a problem. If I declare a struct with a const member, what will happen?For example: if I declared a struct like following: struct{ const int a; char c; }aStruct; then such...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.