473,811 Members | 3,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1515
lp****@yandex.r u 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.r u 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.r u 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.r u 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
4659
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
3308
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 to be in that single file, that worked OK. But when I divdie that file into several ones, I have many "invalid use of undefined type" errors. The four files are main.c, main.h, readLP.h, and readLP.c. In readLP.h
20
2135
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
2044
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 main.c. Now I create a header file which represents the interface of functions.c to my main program file main.c. I put in the header file: all function prototypes with keyword extern and the declarations of the structs, which are defined in the main...
10
2219
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; char nodes; //will be list_node1,list_node2... };
9
2694
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 am confused with the way it is declared and defined. a.h ------- : :
6
2267
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 access label are implicitly private; ifwe usethe struct keyword, then those members are public. Whether we define a class using the class keyword or the struct keyword affects only the default initial access level." Now I defined a struct with...
7
1598
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
1796
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 statement as aStruct.a = 0; is illegal.
0
9731
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10405
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10136
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.