nested-class

Class template specialization in class scope?

◇◆丶佛笑我妖孽 提交于 2019-12-01 15:23:39
Why is the specialization S in A legal and S in B not? ( if B is not commented out ) GCC 4.8.1: error: explicit specialization in non-namespace scope ‘class B’ #include <type_traits> #include <iostream> class Y {}; class X {}; struct A { template<class T, class = void> class S; template<class T> struct S < T, typename std::enable_if< std::is_same< Y, T >::value >::type > { int i = 0; }; template<class T> struct S < T, typename std::enable_if< std::is_same< X, T >::value >::type > { int i = 1; }; }; /* class B { template<class T> class S; template<> class S < Y > {}; template<> class S < X > {}

Class template specialization in class scope?

余生长醉 提交于 2019-12-01 14:17:02
问题 Why is the specialization S in A legal and S in B not? ( if B is not commented out ) GCC 4.8.1: error: explicit specialization in non-namespace scope ‘class B’ #include <type_traits> #include <iostream> class Y {}; class X {}; struct A { template<class T, class = void> class S; template<class T> struct S < T, typename std::enable_if< std::is_same< Y, T >::value >::type > { int i = 0; }; template<class T> struct S < T, typename std::enable_if< std::is_same< X, T >::value >::type > { int i = 1;

Generic base class wraps nested generic class to reduce type argument specification: Is there a name for this pattern?

最后都变了- 提交于 2019-12-01 12:46:55
Ok question title is far from being self-explanatory. I see myself doing this often: From this answer : public static class Equality<T> { public static IEqualityComparer<T> CreateComparer<K>(Func<T, K> keySelector) { return new KeyEqualityComparer<K>(keySelector); } class KeyEqualityComparer<K> : IEqualityComparer<T> { readonly Func<T, K> keySelector; public KeyEqualityComparer(Func<T, K> keySelector) { this.keySelector = keySelector; } public bool Equals(T x, T y) { ---- } public int GetHashCode(T obj) { .... } } } What did I do: There is an implementation detail KeyEqualityComparer<T, K>

Nested class inside an interface

孤街浪徒 提交于 2019-12-01 05:15:38
The java compiler allows me write a Class definition inside an Interface . Are there any specific uses of this ? interface ClassInterface { void returnSomething(); int x = 10; class SomeClass { private int y; private void classDoingSomething() { } } } Please explain . You would use it to tightly bind a certain type to an interface. Read This page for further information and an example of defining a class inside an interface. The uses are the same that a class nested in another class: it allows scoping the class to the interface. You could imagine something like this: public interface Switch {

C# Reflection with recursion

那年仲夏 提交于 2019-11-30 22:18:31
I am working on the Reflection , but i am stuck while doing the recursion. Code : public class User { public string Name; public int Number; public Address Address; } public class Address { public string Street; public string State; public string Country; } now i am printing the values. Type t = user.GetType(); PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo prp in props) { if(!prp.GetType().IsPrimitive && prp.GetType().IsClass) { // Get the values of the Inner Class. // i am stucked over here , can anyone help me with this. Type ty = prp.GetType(); var prpI = ty.GetProperties(

Nested class as a template parameter of parent class in C++

寵の児 提交于 2019-11-30 15:20:50
I want to implement an algorithm as a class deriving from a pure virtual class representing the kind of problem the particular algorithm solves. The general interface would look like this: template<typename A, typename B> class ISolutionToProblem { public: virtual void Init(const A & input, const B & param) = 0; virtual const B & ComputeSolution() = 0; virtual ~ISolutionToProblem() {} }; And the implementation would be for example: template<typename T> class MyAlgorithm: public ISolutionToProblem<typename MyAlgorithm<T>::WorkData, T> { public: struct WorkData { /* Stuff using T... */ };

Putting Nested Classes In Separate Files

你说的曾经没有我的故事 提交于 2019-11-30 14:17:47
I have a file with nested classes, but it's becoming long enough to be unreadable. Is there a way I can break out the nested classes into separate files? Use the right tools. I'm afraid not. As an alternative, you could consider converting some of them to plain Java classes: although nested classes increase encapsulation, you can certainly create good OO design without them. My two cents. Write unit-tests to cover the code you are going to refactor. Then use refactoring tools, e.g. Eclipse/JDT to extract each class. Run the tests for each refactoring job. For extra fun, you can do this without

C++ nested class/forward declaration issue

老子叫甜甜 提交于 2019-11-30 08:07:30
Is it possible to forward-declare a nested class, then use it as the type for a concrete (not pointer to/reference to) data member of the outer class? I.E. class Outer; class Outer::MaybeThisWay // Error: Outer is undefined { }; class Outer { MaybeThisWay x; class MaybeThatOtherWay; MaybeThatOtherWay y; // Error: MaybeThatOtherWay is undefined }; You can't forward-declare a nested class like that. Depending on what you're trying to do, maybe you can use a namespace rather than a class on the outer layer. You can forward-declare such a class no problem: namespace Outer { struct Inner; }; Outer:

C# Reflection with recursion

让人想犯罪 __ 提交于 2019-11-30 05:35:54
问题 I am working on the Reflection , but i am stuck while doing the recursion. Code : public class User { public string Name; public int Number; public Address Address; } public class Address { public string Street; public string State; public string Country; } now i am printing the values. Type t = user.GetType(); PropertyInfo[] props = t.GetProperties(); foreach (PropertyInfo prp in props) { if(!prp.GetType().IsPrimitive && prp.GetType().IsClass) { // Get the values of the Inner Class. // i am

Allow access to but prevent instantiation of a nested class by external classes

a 夏天 提交于 2019-11-30 04:17:32
问题 I'm looking to define a nested class that is accessible to the container class and external classes, but I want to control instantiation of the nested class, such that only instances of the container class can create new instances of the nested class. The proceeding code should hopefully demonstrate this: public class Container { public class Nested { public Nested() { } } public Nested CreateNested() { return new Nested(); // Allow } } class External { static void Main(string[] args) {