static-members

Private class functions vs Functions in unnamed namespace

守給你的承諾、 提交于 2019-11-30 01:13:46
I've found myself that I tend not to have private class functions. If possible, all candidates to private class function rather I put in to unnamed namespace and pass all necessary information as function parameters. I don't have a sound explanation why I'm doing that but at least it looks more naturally to me. As a consequence I need to expose less internal details in the header file. What is your opinion - is it correct practice? In the semi large projects where I usually work (more than 2 million lines of code) I would ban private class functions if I could. The reason being that a private

F# code organization: types & modules

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 23:49:28
How do you decide between writing a function inside a module or as a static member of some type? For example, in the source code of F#, there are lots of types that are defined along with a equally named module, as follows: type MyType = // ... [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] module MyType = // ... Why don't you simply define the operations as static members of type MyType? Here are some notes about the technical distinctions. Modules can be 'open'ed (unless they have RequireQualifiedAccessAttribute). That is, if you put functions ( F and G ) in a

How do you create a static template member function that performs actions on a template class?

心已入冬 提交于 2019-11-29 23:01:27
I'm trying to create a generic function that removes duplicates from an std::vector. Since I don't want to create a function for each vector type, I want to make this a template function that can accept vectors of any type. Here is what I have: //foo.h Class Foo { template<typename T> static void RemoveVectorDuplicates(std::vector<T>& vectorToUpdate); }; //foo.cpp template<typename T> void Foo::RemoveVectorDuplicates(std::vector<T>& vectorToUpdate) { for(typename T::iterator sourceIter = vectorToUpdate.begin(); (sourceIter != vectorToUpdate.end() - 1); sourceIter++) { for(typename T::iterator

How to define a const double inside a class's header file?

梦想与她 提交于 2019-11-29 18:04:36
问题 Inside the header file of my class, I am trying the following and getting compiler complaints: private: static const double some_double= 1.0; How are you supposed to actually do this? 回答1: In C++11, you can have non-integral constant expressions thanks to constexpr : private: static constexpr double some_double = 1.0; 回答2: Declare it in the header, and initialize it in one compilation unit (the .cpp for the class is sensible). //my_class.hpp private: static const double some_double; //my

Where are static class variables stored in memory?

萝らか妹 提交于 2019-11-29 17:47:39
问题 This is a follow-up question to How are static arrays stored in Java memory? . So global variables in C/C++ are stored in the static data segment of memory. But what about static class variables in Java/C++? It can't be the static data segment because you don't know what/how many classes are going to be referenced throughout the duration of your program (because of reflection). It's definitely not the stack because that makes no sense. Storing it on the heap is also kind of iffy. 回答1: In Java

Resolving a linker error: undefined reference to static class members

≡放荡痞女 提交于 2019-11-29 14:45:43
My code is Arduinoish. I turned on verbose compiling so I could verify that all the .o files are indeed getting passed to the linker correctly, and they are (linker command below). This leads me to believe that it is some sort of syntax error. Googling for the error "undefined reference to in function" produces a lot of results with answers like "add foo.o to your linker command like so", etc. I hope the solution is as simple as a missing dot or -> somewhere. I'm getting this series of errors in one file, from the linker: SerialServoControl.cpp.o: In function `SerialServoControl::send(int, int

Protected static member variables

冷暖自知 提交于 2019-11-29 12:27:03
问题 I've recently been working on some class files and I've noticed that the member variables had been set in a protected static mode like protected static $_someVar and accessed like static::$_someVar. I understand the concept of visibility and that having something set as protected static will ensure the member variable can only be accessed in the super class or derived classes but can I access protected static variables only in static methods? Thanks 回答1: If I understand correctly, what you

How do static events compare to non-static events in C#?

情到浓时终转凉″ 提交于 2019-11-29 11:44:37
问题 I just realized static events exist - and I'm curious how people use them. I wonder how the relative comparison holds up to static vs. instance methods. For instance, a static method is basically a global function. But I've always associated events with instances of objects and I'm having trouble thinking of them at the global level. Here some code to refer to if it helps an explanation: void Main() { var c1 = new C1(); c1.E1 += () => Console.WriteLine ("E1"); C1.E2 += () => Console.WriteLine

trying to force static object initialization

冷暖自知 提交于 2019-11-29 10:40:31
I am trying to initialize a static object without success. The purpose is to automatically register a factory class in a repository (which is a singleton). I've already had a look at: How to force a static member to be initialized? One of the comments says that (there is also an example that I've followed): I read it up in the C++ standard (14.7.1): Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the

Why there is no concept of “const-correctness” for class's static member functions?

拟墨画扇 提交于 2019-11-29 09:32:47
Use case: class A { static int s_common; public: static int getCommon () const { s_common; }; }; Typically this results in an error as: error: static member function ‘static int A::getCommon()’ cannot have cv-qualifier This is because const ness applies only to the object pointed by this , which is not present in a static member function. However had it been allowed, the static member function's "const"ness could have been easily related to the static data members. Why is this feature is not present in C++; any logical reason behind it ? cv-qualifiers affect the function's signature. So you