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

Private constructor idiom

PLS
I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.

Thanks,
++PLS
Nov 6 '07 #1
6 3231
On 2007-11-06 15:25:46 -0800, PLS <no****@nowhere.comsaid:
[...]
There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?
You can use the "internal" keyword to achieve similar behavior (though
not identical).

You can also fix it by changing the class hierarchy so that the factory
is combined with the created class, but then you need one factory per
class. That may or may not be desirable.

With the "internal" keyword on the constructor, you'd just make sure
that class A and class B are in the same assembly, then class B can use
the constructor but it would still be hidden from code outsdie that
assembly.

Pete

Nov 7 '07 #2
Neither C# nor VB have anything equivalent to the C++ 'friend' modifier
('Friend' in VB is just equivalent to 'internal' in C#).
You can declare the constructor as internal, but as you know that will only
limit calling the constructor from anywhere within the same project.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
C++ to C++/CLI
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB or C# to C++/CLI
"PLS" wrote:
I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.

Thanks,
++PLS
Nov 7 '07 #3
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.
You could create a static method on class A that creates an instance of
itself and passes this instance to the class factory. Or just decentralise
the class factory and move all that functionality into the individual static
methods.

Michael
Nov 7 '07 #4
That's one of the most common ways of doing it in C#. For example:

public class MyClass
{
private int value;
private MyClass ( int value )
{
this.value = value;
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="value">Initialization parameters</param>
// <returns>New MyClass Object</returns>
public static MyClass Create ( int value )
{
return new MyClass(value);
}
}
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Michael C" wrote:
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.

You could create a static method on class A that creates an instance of
itself and passes this instance to the class factory. Or just decentralise
the class factory and move all that functionality into the individual static
methods.

Michael
Nov 7 '07 #5
Peter Ritchie [C# MVP] <PR****@newsgroups.nospamwrote:
That's one of the most common ways of doing it in C#. For example:

public class MyClass
{
private int value;
private MyClass ( int value )
{
this.value = value;
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="value">Initialization parameters</param>
// <returns>New MyClass Object</returns>
public static MyClass Create ( int value )
{
return new MyClass(value);
}
}
In particular, it can be useful if you've got different units. Look at
TimeSpan.FromSeconds, TimeSpan.FromMilliseconds etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 8 '07 #6
Indeed. One of the patterns prevalent in the .NET BCL is the avoidance of
parameterized constructors in favour of a factory method. Conversion methods
like FromXXX are great examples. It's much more clear to have a
TimeSpan newTimeSpan = TimeSpan.FromMilliseconds(10000);
than
TimeSpan newTimeSpan = new TimeSpan(10000);

Although the TimeSpan class still has public constructors.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Jon Skeet [C# MVP]" wrote:
Peter Ritchie [C# MVP] <PR****@newsgroups.nospamwrote:
That's one of the most common ways of doing it in C#. For example:

public class MyClass
{
private int value;
private MyClass ( int value )
{
this.value = value;
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="value">Initialization parameters</param>
// <returns>New MyClass Object</returns>
public static MyClass Create ( int value )
{
return new MyClass(value);
}
}

In particular, it can be useful if you've got different units. Look at
TimeSpan.FromSeconds, TimeSpan.FromMilliseconds etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 8 '07 #7

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

Similar topics

5
by: pmatos | last post by:
Hi all, I have a style question. I've been for long programming in Lisp-like languages and C when I need a low-level language. Now, I'm programming for professional reasons in C++ (which I had...
10
by: Steve | last post by:
Hi, I thought I had invented a pretty neat solution for my problem, but it turns out I'm using the named constructor idiom. :-) I've seen examples where empty structs are used to identify the...
11
by: fourfires.d | last post by:
Dear All, I am new to c++ and when write below code that try to call copy constructor in "=" operator overloading, it can not compile. Can anyone point out for me the reason? thanks !! ...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
6
by: pragtideep | last post by:
I am trying to understand the following code kindly help me #include<iostream> using namespace std; class base { private: ~base() {cout << "This is base destructor"<<endl ;} friend class...
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
1
by: denis | last post by:
Hello, I noticed the following in the "C++ Coding Standards" book. Why should the code on line 30 be in error? Transmogrify accepts a (B obj), and we are passing it a B obj. I understand why...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
5
by: wiskey5alpha | last post by:
Hello all. I have a design question. assuming we have class Bar {...} // body omitted for clarity class Foo { // should Bar be implemented as a pointer or not here ?
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
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
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...

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.