473,385 Members | 1,356 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,385 software developers and data experts.

friend constructor

Class Foo has a private constructor. How can I give the right to Class Bar
to use that private constructor?
For example, the constructor of System.Windows.Forms.TreeNodeCollection is
private. How can the TreeView create an instance of TreeNodeCollection?
Nov 16 '05 #1
6 2613
Hi Franz,

You can give it access to a static method that will return a new instance of Foo

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

"Morten Wennevik" <Mo************@hotmail.com> ¦b¶l¥ó
news:opr9momhsiklbvpo@morten_x.edunord ¤¤¼¶¼g...
Hi Franz,

You can give it access to a static method that will return a new instance of Foo


I have thought about it. But I just want Bar to be the only class which can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the same
assembly.
Nov 16 '05 #3
n!
> I have thought about it. But I just want Bar to be the only class which
can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the same assembly.


You can make Foo a private class of Bar, and have it implement a public
interface.

n!
Nov 16 '05 #4
You mean this?

public class Foo {
private class Bar : IBar {
void bar() { ........ }
}
}
interface IBar {
void bar();
}

"n!" <nf********@nomailplease.com> ¦b¶l¥ó
news:%2******************@TK2MSFTNGP11.phx.gbl ¤¤¼¶¼g...
I have thought about it. But I just want Bar to be the only class which

can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the

same
assembly.


You can make Foo a private class of Bar, and have it implement a public
interface.

n!

Nov 16 '05 #5
n!
> public class Foo {
private class Bar : IBar {
void bar() { ........ }
}
}
interface IBar {
void bar();
}


Yep, of course you only need the public interface if you want it visible
outside of the containing class.

n!
Nov 16 '05 #6

I used to worry about that sort of thing too, until one day I realised
that if I couldn't trust my own code (i.e. the code in the same assembly)
then I was doomed anyway.....
Simon Smith
simon dot s at ghytred dot com
http://www.ghytred.com/NewsLook - Usenet for Outlook

On 15 Jun 2004 10:24, "Franz" wrote:

"Morten Wennevik" <Mo************@hotmail.com> bl
news:opr9momhsiklbvpo@morten_x.edunord g...
Hi Franz,

You can give it access to a static method that will return a new instance

of Foo


I have thought about it. But I just want Bar to be the only class which can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the same
assembly.



Nov 16 '05 #7

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

Similar topics

5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
2
by: Ruben Lysens | last post by:
Hi, Consider the example below. A Device controls a number of Ports. I'd like the Device to be in charge of creating/deleting Ports. I enforce this by making the Ports constructor and destructor...
4
by: ankit_jain_gzb | last post by:
Hi Iam not able to understand why the following code gives compile problem. Thanks Ankit Jain class B; class A{ public:
3
by: Mark | last post by:
Hi... Maybe I'm just being especially obtuse today, but I've been perplexed by how some of the protection levels have been working with nested classes in C#. I have a parent class that wants to...
1
by: Joe Carner via .NET 247 | last post by:
First time posting, thanks in advance for any help you can give me. Basically I am trying to a class that i want to be able to access the private data members of another class, both of which i...
17
by: Tom | last post by:
I have written a property, where I want to have the GET be available to anyone (i.e. Public); however, I want the SET to be available ONLY to the class or program itself (i.e. Friend). In VB6...
8
by: toton | last post by:
Hi, I have a parser interface , which is class IParser{ public: void readHeader(Document& doc)= 0; void readCC(CC& cc) = 0; }; Now, there are 3 kinds of parser which extends (implements)...
6
by: ralphmerridew | last post by:
My current project involves reading a particular data format. It would be convenient to be able to do something like: class DataClass { friend bool FileRunner::read_data (const...
19
by: subramanian100in | last post by:
Stroustrup, in his book TC++PL 3rd Edition, in page 16, under the section '1.8 Advice' has mentioned the following: Don't use global data(use members) Don't use global functions Don't use...
5
by: mc | last post by:
Hello, I'm trying to create a class (A) with a default constructor friend to another one (B) as follows: class A { public: A(int a); ~A();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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...

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.