473,569 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What're the benift and the drawback of defining a class embedded inside another class.

Hi, all.

What are the benefit and the drawback of defining a class embedded inside
another class?

For example:

class List
{
public:
int count;

class iterator
{
int index;
};
};

Is there any limitation on such a defination? Usually, under what kinds of
situation do we use it?

Thanks.

Best Regards,

Xiangliang Meng
Jul 23 '05 #1
5 1924

Xiangliang Meng wrote:
What are the benefit and the drawback of defining a class embedded inside another class? <snip example> Is there any limitation on such a defination? Usually, under what kinds of situation do we use it?


The worst drawback I've found in using this sort of syntax is that you
can't forward declare the contained class, so if you need to do

class List; //OK
class List::iterator; //Syntax error

you can't. It is stunningly annoying.

Jul 23 '05 #2
ThosRTanner wrote:
The worst drawback I've found in using this sort of syntax is that you
can't forward declare the contained class, so if you need to do

class List; //OK
class List::iterator; //Syntax error

you can't. It is stunningly annoying.

May you provide an example where this would be useful? I think you can't forward declare
the contained class for valid reasons. How the compiler could check whether the
declaration is correct, before reaching the class definition?
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
"Ioannis Vranos" <iv*@remove.thi s.grad.com> wrote in message
news:1113211439 .505186@athnrd0 2
ThosRTanner wrote:
The worst drawback I've found in using this sort of syntax is that
you can't forward declare the contained class, so if you need to do

class List; //OK
class List::iterator; //Syntax error

you can't. It is stunningly annoying.

May you provide an example where this would be useful?


It is useful for the same reasons it is useful with ordinary classes.
Suppose you have two inner classes, Inner1 and Inner2, and you want each to
store a pointer to the other.

You can, however, work around this problem by only forward declaring the
inner classes within the outer class, e.g.,

class Outer
{
class Inner1;
class Inner2;
};

class Outer::Inner1
{
Inner2 *ptr;
};

class Outer::Inner2
{
Inner1 *ptr;
};
I think you
can't forward declare the contained class for valid reasons. How the
compiler could check whether the declaration is correct, before
reaching the class definition?


That is no doubt the reason why it is not allowed. It is nevertheless
somewhat inconvenient.

--
John Carson

Jul 23 '05 #4
John Carson wrote:
May you provide an example where this would be useful?

It is useful for the same reasons it is useful with ordinary classes.
Suppose you have two inner classes, Inner1 and Inner2, and you want each
to store a pointer to the other.

You can, however, work around this problem by only forward declaring the
inner classes within the outer class, e.g.,

class Outer
{
class Inner1;
class Inner2;
};

class Outer::Inner1
{
Inner2 *ptr;
};

class Outer::Inner2
{
Inner1 *ptr;
};

I think this is a different case from the original.
That is no doubt the reason why it is not allowed. It is nevertheless
somewhat inconvenient.

Yes it is inconvenient. However I think it is for reasonable reasons. :-)

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5

Ioannis Vranos wrote:
ThosRTanner wrote:
The worst drawback I've found in using this sort of syntax is that you can't forward declare the contained class, so if you need to do

class List; //OK
class List::iterator; //Syntax error

you can't. It is stunningly annoying.

May you provide an example where this would be useful? I think you

can't forward declare the contained class for valid reasons.
Given the existing convention that iterators are sub-classes of the
container over which they iterate, then

class Container
{
//...
class iterator
{
//...
}
//...
}

is not an unlikely scenario.

Under certain circumstances, it can be necessary for the iterator to
need to be a friend of the class contained in the container (don't ask,
it just is), and there is no way to order the declarations and stuff to
enable avoid forward declaring either the iterator class (which you
can't) or a proxy class which just wraps the iterator (which is a real
waste of time (and extra code to maintain, etc)). Changing the order of
declaration isn't applicable for various reasons.
How the compiler could check whether the
declaration is correct, before reaching the class definition?


In answer to your 2nd question - presumably the compiler determines
that the forward declaration is valid in exactly the same way it
determines that a forward declaration of class is valid. i.e. it
doesn't, it assumes that it is, until it finds another declaration of
Container::iter ator which isn't a class. For instance, this is going to
be considered valid:

class Wibble;
std::ostream &std::operator< <(str::ostrea m &, Wibble const &w);

until the compiler finds a declaration of Wibble which isn't a class. I
can't see why a subclass in here would confuse the compiler any more
(or less).

class Wibble;
class Wibble::Sub;
std::ostream &std::operator< <(str::ostrea m &, Wibble::Sub const
&w);

I thought perhaps if Wibble::Sub was private, but even then the
implementation is going to need to get hold of the real definition of
Wibble::Sub and the compiler will error at that point (as indeed it
will if Wibble isn't a class, etc, etc). I'd really like to see the
reason why this isn't permitted.

Jul 23 '05 #6

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

Similar topics

1
1864
by: tony | last post by:
hi. is there a way to find inside my class.method , what is the class name that call this method ? i know i can send parameter as : Me.GetType.FullName()
6
4758
by: Muscha | last post by:
Hello, I have the following exception in my app, have been chasing it up for the past few days none to avail. Help does anyone know what this may be? Unhandled Exception: System.IO.IOException: Unable to read data from the transpo rt connection. ---> System.ObjectDisposedException: Cannot access a disposed obj ect.
4
1392
by: Art | last post by:
Hi, I want to write an error handler for my project. I would call this handler and have it print a message. What I'd like to be able to do is to have the handler know which class called it. Also, since the class that called it may have been passed bad data from another class, I'd like to pass the name of the other class also (if...
9
5989
by: Steve | last post by:
Hello -- I've been struggling with this problem for over a day now. I'd like to know (without passing parameters) which class, and preferably, which method of that class has called my function. I can get the calling assembly rather easily, but let's say within Assembly X I have class A
0
1453
by: martin | last post by:
Hello, I have raw XML that I want to package as a SOAP message and send over HTTP to some web service end-point. Later on I'm gonna need it to be wrapped into a SSL tunnel too, with full WS-Security support. What class is easiest for me to use? (I can not use the add webreference / generate proxy class thing because my program needs to...
4
2266
by: C# | last post by:
hi what class can seize http request in .NET framework or c#? thinks!!
2
2016
by: ad | last post by:
I use a pre-defined CSS to my webpage. There are many class in the CSS. The header of my GridView will be affected by the CSS, but I can't find the class which affect the header of the GridView. What class in CSS affect the head of GridView?
13
1663
by: GTi | last post by:
I need to figure out what kind of class a object is: public class ClassA { public string Name = null; } public class ClassB { public string Name = null;
4
1822
by: Gilbert Tordeur | last post by:
Hello, My program is creating pdf files, around 50 - 100 kB each. Today I write them in text files, then read them back to store into a database. It's obvious for me that I can save the text file operations by creating a big string in memory (only append), but it's unclear what class to use : String StringBuilder, Stream something or ? ...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
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...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
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...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.