member

What is correct way to initialize a static member of type 'T &' in a templated class?

自作多情 提交于 2019-12-01 20:12:44
I'm playing around with an eager-initializing generic singleton class. The idea is that you inherit publicly from the class like so: class foo : public singleton<foo> { }; I've learned a lot in the process but I'm stuck right now because it's breaking my Visual Studio 2008 linker. The problem is with the static instance member and/or its initialization. template<class T> class singleton { singleton(); singleton(singleton const &); singleton & operator = (singleton const &); public: static T & instance; }; template<class T> T & T::instance; Any insight would be greatly appreciated! EDIT: With

What is correct way to initialize a static member of type 'T &' in a templated class?

旧城冷巷雨未停 提交于 2019-12-01 19:47:47
问题 I'm playing around with an eager-initializing generic singleton class. The idea is that you inherit publicly from the class like so: class foo : public singleton<foo> { }; I've learned a lot in the process but I'm stuck right now because it's breaking my Visual Studio 2008 linker. The problem is with the static instance member and/or its initialization. template<class T> class singleton { singleton(); singleton(singleton const &); singleton & operator = (singleton const &); public: static T &

C# binarysearch a list<T> by a member of T

懵懂的女人 提交于 2019-12-01 17:45:11
I have a baseclass Event with a DateTime member TimeStamp . Lots of other event-classes will derive from this. I want to be able to search a list of events fast, so I'd like to use a binary search. (The list-data is sorted on timestamp, but there might be duplicate timestamps for events that occurred simultaneously) So I started out writing something like this : public class EventList<T> : List<T> where T : Event { private IComparer<T> comparer = (x, y) => Comparer<DateTime>.Default.Compare(x.TimeStamp, y.TimeStamp); public IEnumerable<T> EventsBetween(DateTime inFromTime, DateTime inToTime) {

C# accesing non static member in a static function

主宰稳场 提交于 2019-12-01 17:17:09
So I have a function: List<string> names = new string(); private static void getName(string name) { names.add(name); } When I attempt to compile I get a: 'object reference is required for the non-static field' notice. What do I have to do to make this member (names) compatible with getName? I need it to be non static or converted because I want to put the results into other non static functions and forms. You need to tell the system which list of names you're interested in. It's part of the state of an object, an instance of the class... but which one? Maybe you've created several instances of

C# binarysearch a list<T> by a member of T

我与影子孤独终老i 提交于 2019-12-01 17:12:43
问题 I have a baseclass Event with a DateTime member TimeStamp . Lots of other event-classes will derive from this. I want to be able to search a list of events fast, so I'd like to use a binary search. (The list-data is sorted on timestamp, but there might be duplicate timestamps for events that occurred simultaneously) So I started out writing something like this : public class EventList<T> : List<T> where T : Event { private IComparer<T> comparer = (x, y) => Comparer<DateTime>.Default.Compare(x

Is `this` keyword optional when accessing members in C#?

为君一笑 提交于 2019-12-01 16:33:10
I notice that if you have a private member in a class, you can access it in the class methods by just referring to it's name. You do not need to say this.memberName , just memberName works. So is the this keyword optional in the context of member access? I do see it is useful when you want to clarify the scope - when you have 2 variables with the same name. Is there any other reason to use it when accessing members? Yes, it's optional. The only times you'd have to use it are when you have a local variable that hides a member variable, or you want to refer to an indexed property (aka indexer) .

C# accesing non static member in a static function

故事扮演 提交于 2019-12-01 16:28:41
问题 So I have a function: List<string> names = new string(); private static void getName(string name) { names.add(name); } When I attempt to compile I get a: 'object reference is required for the non-static field' notice. What do I have to do to make this member (names) compatible with getName? I need it to be non static or converted because I want to put the results into other non static functions and forms. 回答1: You need to tell the system which list of names you're interested in. It's part of

C++: syntax for accessing member struct from pointer to class

核能气质少年 提交于 2019-12-01 16:05:53
I'm trying to access a member structs variables, but I can't seem to get the syntax right. The two compile errors pr. access are: error C2274: 'function-style cast' : illegal as right side of '.' operator error C2228: left of '.otherdata' must have class/struct/union I have tried various changes, but none successful. #include <iostream> using std::cout; class Foo{ public: struct Bar{ int otherdata; }; int somedata; }; int main(){ Foo foo; foo.Bar.otherdata = 5; cout << foo.Bar.otherdata; return 0; } You only define a struct there, not allocate one. Try this: class Foo{ public: struct Bar{ int

C++: syntax for accessing member struct from pointer to class

无人久伴 提交于 2019-12-01 15:25:03
问题 I'm trying to access a member structs variables, but I can't seem to get the syntax right. The two compile errors pr. access are: error C2274: 'function-style cast' : illegal as right side of '.' operator error C2228: left of '.otherdata' must have class/struct/union I have tried various changes, but none successful. #include <iostream> using std::cout; class Foo{ public: struct Bar{ int otherdata; }; int somedata; }; int main(){ Foo foo; foo.Bar.otherdata = 5; cout << foo.Bar.otherdata;

Why protected superclass member cannot be accessed in a subclass function when passed as an argument?

廉价感情. 提交于 2019-12-01 15:14:40
I get a compile error, which I'm slightly confused about. This is on VS2003. error C2248: 'A::y' : cannot access protected member declared in class 'A' class A { public: A() : x(0), y(0) {} protected: int x; int y; }; class B : public A { public: B() : A(), z(0) {} B(const A& item) : A(), z(1) { x = item.y;} private: int z; }; The problem is with x = item.y; The access is specified as protected. Why doesn't the constructor of class B have access to A::y? It's because of this: class base_class { protected: virtual void foo() { std::cout << "base::foo()" << std::endl; } }; class A : public base