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

Singleton object in a managed C++

Hello,

In C# I'm using the foloowing line sysnax for singleton instantiation:
public static readonly MyClass instance = new MyClass();

I wish to do the same in a managed C++, but I I get all kind of errors for
this line and I can't fix it.

How do I write a singleton class in MCC?
--------
Thanks
Sharon
Dec 16 '07 #1
8 5888
Sharon wrote:
Hello,

In C# I'm using the foloowing line sysnax for singleton instantiation:
public static readonly MyClass instance = new MyClass();

I wish to do the same in a managed C++, but I I get all kind of
errors for this line and I can't fix it.

How do I write a singleton class in MCC?
MCC == C++ CLI?

If yes then just use initonly and gcnew:

ref class MyClass
{
public:
static initonly MyClass^ instance = gcnew MyClass;
...
};
Check http://www.yoda.arachsys.com/csharp/singleton.html for more details and
variants. Also take a look to BeforeFieldInit attribute.
Regards

--
Cholo Lennon
Bs.As.
ARG
Dec 17 '07 #2

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:B0**********************************@microsof t.com...
Hello,

In C# I'm using the foloowing line sysnax for singleton instantiation:
public static readonly MyClass instance = new MyClass();

I wish to do the same in a managed C++, but I I get all kind of errors for
this line and I can't fix it.

How do I write a singleton class in MCC?
In C++, static member variables that are not integral constants need to be
declared and initialized outside the class.

class C {
static C instance;
};

C C::instance(/* constructor args go here */);

In a ref class, you would use the type initializer to set up static members.
>

--------
Thanks
Sharon

Dec 17 '07 #3
Great, thanks guys,
I find it all except the initonly.
Why the initonly attribute is not simply the same as the in C#? Why not
simply supporting the readonly attribute in MC++ as well?

I suppose the line is a thread safe like in the C# case.

public:
static initonly MyClass^ Instance = gcnew MyClass();

Something else I'm wondering about:
I know it's not really needed, but I tried to add property key word to this
line, but I get a lot of errors. Why is that?
---------
Thanks
Sharon
Dec 18 '07 #4
Sharon wrote:
Why the initonly attribute is not simply the same as the in C#? Why
not simply supporting the readonly attribute in MC++ as well?
Maybe because C++ CLI isn´t C# ;-)
C++ CLI is a big and complicated language, with differents memory models and
internal rules. Isn´t easy to map one to one feature of C# with C++ CLI.
I suppose the line is a thread safe like in the C# case.

public:
static initonly MyClass^ Instance = gcnew MyClass();
AFAIK only if MyClass class doesn´t have 'BeforeFieldInit' attribute

>
Something else I'm wondering about:
I know it's not really needed, but I tried to add property key word
to this line, but I get a lot of errors. Why is that?
Did you mean something like this?

ref class MyClass {
// If you use a getter for 'instance' you can avoid 'initonly' keyword
static MyClass^ instance = gcnew MyClass();

public:
...

static property MyClass^ Instance
{
MyClass^ get()
{
return instance;
}
}
...
};
Regards

--
Cholo Lennon
Bs.As.
ARG

Dec 18 '07 #5
Of course C++ CLI and C++ are different, but the definition for the initonly
in MC++ is the same as readonly in C# (isn't it?). So I thought that it will
nice to keep the same syntax for the same definition.

Yes, using the property as you poster is the same as I did, but I was
wondering why can't I use the properly as a keyword only in the same line of
the static veritable.
nevertheless, I do not need it as a property, I simply moved this static
variable as public.

--
Thanks
Sharon
Dec 18 '07 #6

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:BC**********************************@microsof t.com...
Of course C++ CLI and C++ are different, but the definition for the
initonly
in MC++ is the same as readonly in C# (isn't it?). So I thought that it
will
nice to keep the same syntax for the same definition.
Function and syntax are totally different things. Also C++ existed long
before C#, so it is evidently C# that is in error for not using the accepted
syntax. While we're at it, let's everyone use the assembler syntax for data
segments, since this performs the same function as variable initialization
and it's nice to keep the same syntax ;)

Yes, the C++ syntax is different, but it's not any harder, you just have to
get used to it, like you got used to C# syntax. I learned C++ first, so
it's more natural for me to do things the C++ way.
>
Yes, using the property as you poster is the same as I did, but I was
wondering why can't I use the properly as a keyword only in the same line
of
the static veritable.
nevertheless, I do not need it as a property, I simply moved this static
variable as public.

--
Thanks
Sharon

Dec 18 '07 #7
This is not what I meant.

In the native C++ you have the static and const which have the same
functionality as readonly and static of the C#.

But MC++ support both native C++ and MC++, therefore it's understood why the
additional words (for the CLI).
But since there is the attribute names for the CLR, why inventing alias for
it?

Note that there are many keywords that are common for C# and MC++.

P.S. I also came form 7 years in C++ world and only then to the C# world...
------
Thanks
Sharon
Dec 18 '07 #8

"Sharon" <Sh*****@newsgroups.nospamwrote in message
news:D9**********************************@microsof t.com...
This is not what I meant.

In the native C++ you have the static and const which have the same
functionality as readonly and static of the C#.
C# readonly is nothing at all like C++ const. C# const is nothing like C++
const. C# doesn't have any sort of support for const-correctness.
>
But MC++ support both native C++ and MC++, therefore it's understood why
the
additional words (for the CLI).
But since there is the attribute names for the CLR, why inventing alias
for
it?
You'd have to ask the C# team that, "initonly" is the term used in MSIL
which defines the CLR.

My guess -- "initonly" is far too descriptive, decreasing the need for
clarification and thus for selling training books and courses.
>
Note that there are many keywords that are common for C# and MC++.
And C# has abused almost all of them by introducing random inconsistencies.
>
P.S. I also came form 7 years in C++ world and only then to the C#
world...
------
Thanks
Sharon

Dec 18 '07 #9

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

Similar topics

2
by: Rajarshi Guha | last post by:
Hi, I'm having a little problem with understanding the working of a singleton and borg class. Basically I nedd an class whose state will be shared across several modules. I found the stuff on the...
26
by: Uwe Mayer | last post by:
Hi, I've been looking into ways of creating singleton objects. With Python2.3 I usually used a module-level variable and a factory function to implement singleton objects. With Python2.4 I...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
2
by: Dan | last post by:
All, I am developing an Object that wraps an old C library. Baeed upon how the object will be used I decided the best bet would be to make the object a singleton. Additionally, because the...
7
by: Adrian | last post by:
Hi, I have a large unmanaged static C++ library which I've wrapped using a small C++/CLR DLL. This is called from a C# client application. The static library has a singleton, however it appears...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
8
by: tech | last post by:
Is it ok to allow another object to own a singleton object, or is this definitely a NO NO. I have a utility class that i want to provide access to a whole group of subobjects so i can make this...
0
by: George Sakkis | last post by:
On Jul 3, 6:58 pm, Urizev <uri...@gmail.comwrote: Because __init__() is called to initialize the state of an object *after* it has already been created. You should create a "new-style" class...
4
by: joes.staal | last post by:
Hi, I know this has been asked earlier on, however, none of the other threads where I looked solved the following problem. 1. I've got a native C++ library (lib, not a dll) with a singleton. 2....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.