static-members

Class decorator to declare static member (e.g., for log4net)?

不问归期 提交于 2019-12-23 09:56:29
问题 I'm using log4net, and we have a lot of this in our code: public class Foo { private static readonly ILog log = LogManager.GetLogger(typeof(Foo)); .... } One downside is that it means we're pasting this 10-word section all over, and every now and then somebody forgets to change the class name. The log4net FAQ also mentions this alternative possibility, which is even more verbose: public class Foo { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase

Class decorator to declare static member (e.g., for log4net)?

二次信任 提交于 2019-12-23 09:55:17
问题 I'm using log4net, and we have a lot of this in our code: public class Foo { private static readonly ILog log = LogManager.GetLogger(typeof(Foo)); .... } One downside is that it means we're pasting this 10-word section all over, and every now and then somebody forgets to change the class name. The log4net FAQ also mentions this alternative possibility, which is even more verbose: public class Foo { private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase

using static methods of a constrained generic type C#

南楼画角 提交于 2019-12-23 08:10:10
问题 I have a generic class: public class Foo<T> where T: Interface { } the interface that T is being forced to implement has 2 static methods defined inside of it. in the constructor I want to be able to basically do the following: public Foo() { value1 = T.staticmethod1(); value2 = T.staticmethod2(); } This cannot be accomplished with the psuedocode I have posted above. Is it not possible to call these static methods in this way? 回答1: You may be able to use extension methods. This technique has

Is there any way by which I can save the state of `static members`?

孤街醉人 提交于 2019-12-23 07:37:08
问题 Just like the way we save the instance variables using serialization, is there any way by which I can save the state of static members? If there is a situation, where getting back the state of static members is necessary to restore something, how would one do that? 回答1: The easiest option that comes to my mind is to use a singleton rather than static fields. The singleton object can be serialized and deserialized, and you can manage its lifetime, while you preserve the 'global state' that

java objects, shared variables

六月ゝ 毕业季﹏ 提交于 2019-12-23 02:34:56
问题 I have a simple question here. If I declare a variable inside an object which was made [declared] in the main class, like this: public static int number; ( usually I declare it like this : private int number; ) can it be used in a different object which was also made [declared] in the main class? btw I do not care about security atm, I just want to make something work, don't care about protection) 回答1: Here's a telling quote from Java Language Specification: JLS 8.3.1.1 static Fields If a

Initializing static members of a templated class

◇◆丶佛笑我妖孽 提交于 2019-12-22 05:36:09
问题 I'm trying to figure out why this example doesn't compile. My understanding is that if a static variable is not explicitly set then it defaults to 0. In the five examples below four of them behave as I would expect, but the one that's commented out won't compile. #include <iostream> class Foo { public: static int i; static int j; }; template <int n> class Bar { public: Bar(int) { } static int i; }; static int i; int Foo::i; int Foo::j = 1; template <> int Bar<2>::i; template <> int Bar<3>::i

Is a class with only static methods preferable to a namespace?

风流意气都作罢 提交于 2019-12-22 05:08:30
问题 I was inspired by the comments under this question. I didn't see any reason why a class with only static functions would be a better design than a namespace (with just functions). Any list of pros and cons of these two approaches are welcomed. It would be great with some practical examples! 回答1: One non-stylistic difference is that you can use a class as a template parameter, but you cannot use a namespace. This is sometimes used for policy classes, like std::char_traits. Outside of that use

Why does C# compiler overload resolution algorithm treat static and instance members with equal signature as equal?

左心房为你撑大大i 提交于 2019-12-22 02:06:06
问题 Let we have two members equal by signature, but one is static and another - is not: class Foo { public void Test() { Console.WriteLine("instance"); } public static void Test() { Console.WriteLine("static"); } } but such code generate brings a compiler error: Type 'Foo' already defines a member called 'Test' with the same parameter types But why? Let we compiled that successfully, then: Foo.Test() should output "static" new Foo().Test(); should output "instance" Can't call the static member

Can't a class have static constexpr member instances of itself?

孤者浪人 提交于 2019-12-22 01:23:10
问题 This code is giving me incomplete type error. What is the problem? Isn't allowed for a class to have static member instances of itself? Is there a way to achieve the same result? struct Size { const unsigned int width; const unsigned int height; static constexpr Size big = { 480, 240 }; static constexpr Size small = { 210, 170 }; private: Size( ) = default; }; 回答1: Is there a way to achieve the same result? By "the same result", do you specifically intend the constexpr -ness of Size::big and

Don't static members make classes kind of (global) objects themselves?

不羁岁月 提交于 2019-12-21 20:02:55
问题 Every time I come across an implementation of the singleton pattern or any static classes (i.e. classes with (almost) only static members) I wonder whether this isn't actually a hack and therefore heavy abuse of the principle of classes and instances just to design single objects instead of designing classes and creating a single instance. To me, it looks like static members of classes in general try to add some sort of characteristics to classes which they actually aren't supposed to have