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

Is it possible to have a static field of a struct type of its type

That didn't come out right, but what I mean is something like:

struct X
{
private int x ;

public readonly X MinValue ;

static X() { MinValue = new X ( 0 ) ; }

public X
(
int x
)
{
this.x = x ;
}
}

The struct has a readonly field to contain the MinValue, and the type of
that field has to be that the struct that's being defined. Ergo I get
"...causes a cycle in the struct layout". I.e. I can't construct the static
value until after the static value is constructed.

So I'm left with making a read-only property for it, which works, but takes
more processing each time one of these "constants" is accessed.

Is there something I'm missing?

Another solution would be to store the minvalue in a private readonly
_object_ and have a property that simply casts it back, that would cut down
the processing.

Any thoughts?

Jan 5 '06 #1
4 1385
PIEBALD <PI*****@discussions.microsoft.com> wrote:
That didn't come out right, but what I mean is something like:
<snip>
So I'm left with making a read-only property for it, which works, but takes
more processing each time one of these "constants" is accessed.

Is there something I'm missing?


Yup - and I suspect you'll kick yourself. The problem is that you
haven't declared MinValue to be static. (You clearly know it should be,
given the subject of your post :)

Make MinValue static and it works fine.

--
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
Jan 5 '06 #2
Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:

struct X
{
private object minvalue = null ;

....

public static X MinValue
{
get
{
if ( minvalue == null )
{
// set minvalue
}

return ( (X) minvalue ) ) ;
}
}

}

Which may be better if a lot of processing goes into calculating the value,
because it won't be calculated if it's never accesssed, and only once if it
is.
Jan 6 '06 #3
PIEBALD <PI*****@discussions.microsoft.com> wrote:
Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:


<snip>

I suggest you have a look at
http://www.pobox.com/~skeet/csharp/singleton.html

Even though you're not implementing a singleton as such, things like
thread safety and laziness are still relevant.

--
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
Jan 6 '06 #4
PIEBALD wrote:
Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:


<snip>

At that stage, I suggest you read
http://www.pobox.com/~skeet/csharp/singleton.html for a few caveats and
suggestions.

(You're not actually implementing a singleton, but the discussions on
laziness and thread safety are likely to be useful.)

Jon

Jan 7 '06 #5

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

Similar topics

5
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
11
by: Rob Williscroft | last post by:
I have the following code , part of a trait to detect wether a type has a member operator "Type" (), it works fine with g++ 3.4 (msvc compiles it but it doesn't work for template'd operator T ())...
13
by: Eric | last post by:
I have a template class that instantiates a variable of the template parameter type as follows: template <class T> struct TemplateClass { T t; }; struct TestClass
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
3
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any...
8
by: Tomás | last post by:
Given the following: static_cast<T>( expr ) This will evaluate to an l-value if T is a reference type -- otherwise it will evaluate to an r-value. The same goes for reinterpret_cast. ...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
15
by: archana | last post by:
Hi all, can anyone tell me differene between public static and private static method. how they are allocated and access?. thanks in advance.
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.