Singleton object in a managed C++

S

Sharon

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?
 
C

Cholo Lennon

Sharon said:
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
 
B

Ben Voigt [C++ MVP]

Sharon said:
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.
 
S

Sharon

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?
 
C

Cholo Lennon

Sharon said:
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
 
S

Sharon

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.
 
B

Ben Voigt [C++ MVP]

Sharon said:
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.
 
S

Sharon

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...
 
B

Ben Voigt [C++ MVP]

Sharon said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top