473,398 Members | 2,403 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,398 software developers and data experts.

Nested Class

Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.

Nov 17 '05 #1
4 1813
I don't think that's possible. The inner class's constructor must
have at least internal visibility so that the outer class can
instantiate it, but internal visibility allows instantiation by other
classes in the same assembly.

On 8 Nov 2005 07:07:23 -0800, "UncleJoe" <un************@yahoo.ca>
wrote:
Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.

--
http://www.kynosarges.de
Nov 17 '05 #2

"UncleJoe" <un************@yahoo.ca> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Hi all,

What is the syntax to declare a nested class that can only be
instantiated by the outer class, yet visible to the other classes?
Essentially I want to limit the instantiation and access of the nested
class through the outer class. Thanks for your help in advance.


The best way is to probably define a public interface for your private nested class and the simply
return the interface.

Try this

using System;

class Test
{
public static void Main()
{
Outer outer = new Outer();
IX ix = outer.GetX();
ix.Foo();
}

public interface IX
{
void Foo();
}

public class Outer
{
public IX GetX()
{
return (new Nested());
}

private class Nested : IX
{
public void Foo()
{
Console.WriteLine("in Nested.Foo()");
}
}
}
}
Nov 17 '05 #3
Beautiful! Exactly what I needed. Thanks.

Nov 17 '05 #4
Hi,

UncleJoe wrote:
What is the syntax to declare a nested class that
can only be instantiated by the outer class, yet
visible to the other classes?


You can put the two classes in a separate assembly and declare the Inner
class constructor "internal". If you are creating some kind of a reusable
library already, this could be exactly what you want.

You can also declare a public interface and have your Inner class declared
within the Outer class as private, but implementing the public interface.
With this combination, you can only instantiate objects of the Inner class
within the Outer class, but still expose the objects through the outer class
(as being type of the interface).

The result is more or less what you want, in either case.

--
Chris Priede
Nov 17 '05 #5

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

Similar topics

3
by: Erik Bongers | last post by:
Hi, Nested classes only seem to be able to access static members of the surrounding class : class SurroundingClass { public: class InnerClass { public:
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
2
by: newbiecpp | last post by:
Java can declare a static nested class. Does C++ have same thing like? class Outer { public: static class Inner { ... }; .... };
6
by: B0nj | last post by:
I've got a class in which I want to implement a property that operates like an indexer, for the various colors associated with the class. For instance, I want to be able to do 'set' operations...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
9
by: Joel Moore | last post by:
I'm a little confused here. If I have the following: Public ClassA Friend varA As Integer Private varB As Integer Private ClassB Public Sub MethodA() ' How can I access varA and varB here?...
1
by: Tomas Sieger | last post by:
Hi all, I'm in doubt with the following code: class Base { public: class Nested {}; }; class Derived:public Base { public: class Nested {
3
by: jdurancomas | last post by:
Dear all, I'm trying to declare the operator++ to a nested class. The nested class is not template but the container it is. The code used in teh sample program is included bellow: ...
3
by: Cousson, Benoit | last post by:
I don't think so; my original email was mainly a question. I do agree that they are other ways to do what I'm trying to achieve; there are always several ways to solve an issue. Few days ago, I...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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,...
0
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...

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.