当我编译这个:
#ifndef BTREE_H
#define BTREE_H
#include <QList>
template <class T, int degree>
class btree
{
public:
class node
{
public :
node();
private:
node* parent;
QList<T> values;
QList<node*> children;
};
public:
btree();
void insert(const T& value);
node* findLeaf(const T& value);
void performInsertion(const T& value, node& place);
//
node* root;
};
#endif // BTREE_H
Run Code Online (Sandbox Code Playgroud)
findLeaf的实现是这样的:
template <class T, int degree>
btree<T,degree>::node* btree<T,degree>::findLeaf(const T &value)
{
if(root == NULL)
return root;
}
Run Code Online (Sandbox Code Playgroud)
发生此错误:
error: need ‘typename’ before ‘btree<T, degree>::Node’
because ‘btree<T, degree>’ …Run Code Online (Sandbox Code Playgroud) 假设我有一些嵌套特征:
trait Foo { trait Bar }
Run Code Online (Sandbox Code Playgroud)
还有几个例子:
val myFoo = new Foo {}
val myBar = new myFoo.Bar {}
Run Code Online (Sandbox Code Playgroud)
我可以写下面的内容,看起来(至少一目了然)就像他们应该或多或少地做同样的事情:
def whatever1(foo: Foo)(bar: foo.Bar) = bar
def whatever2(foo: Foo): foo.Bar => foo.Bar = { bar => bar }
def whatever3(foo: Foo) = new { def apply(bar: foo.Bar) = bar }
case class whatever4(foo: Foo) { def apply(bar: foo.Bar) = bar }
case class whatever5[F <: Foo](foo: F) { def apply(bar: foo.Bar) = bar }
Run Code Online (Sandbox Code Playgroud)
请注意,最后一个受到此处给出的解决方案的启发.
前三项工作: …
types scala nested-class path-dependent-type dependent-method-type
考虑以下:
template<typename X>
struct Z {};
struct A
{
using Z = ::Z<int>;
struct B : Z
{
using C = Z;
};
};
Run Code Online (Sandbox Code Playgroud)
编译好了.尼斯.但现在添加另一个参数Z:
template<typename X, typename Y>
struct Z {};
struct A
{
template<typename X>
using Z = ::Z<X, int>;
struct B : Z<B>
{
using C = Z<B>; // error: too few template arguments for class template 'Z'
};
};
Run Code Online (Sandbox Code Playgroud)
好吧,也许有意义的是,在派生嵌套类时Z,类中模板别名的定义A是可见的B,但在其体内却不可见,因为全局定义Z有两个参数,所以触发错误.
但是为什么第一种情况下的行为不同,何时Z只是一个类型别名A …
有没有办法限制C#中嵌套类的实例化?我想防止嵌套类从除嵌套类之外的任何其他类实例化,但允许从其他代码完全访问嵌套类.
如果我com.example.test.Enum2.Test在下面的代码中有一个类,为什么getCanonicalName()返回com.example.test.Enum2.Test但Class.forName()需要"com.example.test.Enum2$Test"作为参数?
有没有办法保持一致,这样我可以通过名称序列化/反序列化枚举值,而不必检查每个$vs .可能性,当枚举是一个嵌套类?
package com.example.test;
import java.util.Arrays;
public class Enum2 {
enum Test {
FOO, BAR, BAZ;
}
public static void main(String[] args) {
for (String className : Arrays.asList(
"com.example.test.Enum2.Test",
"com.example.test.Enum2$Test"))
{
try {
Class<?> cl = Class.forName(className);
System.out.println(className+" found: "+cl.getCanonicalName());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
System.out.println(Test.FOO.getDeclaringClass().getCanonicalName());
}
}
Run Code Online (Sandbox Code Playgroud)
澄清:我正在寻找一种在实际应用中处理这个问题的好方法(不仅仅是上面提到的测试用例),或者:
一个.使用getCanonicalName()输出序列化/反序列化(仅点缀名称),并Class.forName()依次尝试每种可能性,例如"com.example.test.Enum2.Test",然后"com.example.test.Enum2$Test",然后"com.example.test$Enum2$Test",等等.
湾 使用正确的$表示法,这样 …
我已经看到了一些"解决方案",但每次解决方案似乎都是"不要使用嵌套类,在外面定义类,然后正常使用它们".我不喜欢这个答案,因为它忽略了我选择嵌套类的主要原因,也就是说,要创建一个可以访问所有子类实例的常量池(与基类相关联).
这是示例代码:
class ParentClass:
constant_pool = []
children = []
def __init__(self, stream):
self.constant_pool = ConstantPool(stream)
child_count = stream.read_ui16()
for i in range(0, child_count):
children.append(ChildClass(stream))
class ChildClass:
name = None
def __init__(self, stream):
idx = stream.read_ui16()
self.name = constant_pool[idx]
Run Code Online (Sandbox Code Playgroud)
所有类都传递一个param,这是一个自定义比特流类.我的目的是找到一个解决方案,它不需要我在ChildClass中读取ChildClass的idx值.所有子类流读取都应该在子类中完成.
这个例子过于简化了.常量池不是我需要的所有子类的唯一变量.idx变量不是从流阅读器读取的唯一内容.
这在python中甚至可能吗?有没有办法访问父母的信息?
我看到大多数一直在玩ScalaSigParser的人,为了以一种很好的方式服务于惯用的Scala案例类,已经避免了这个问题,但我想知道它是否可行.我的情况很像以下情况:
trait OuterTrait {
abstract class InnerAbstract(i: Int)
}
object OuterObject extends OuterTrait {
case class InnerConcrete(i: Int) extends InnerAbstract(i)
}
val bippy = OuterObject.InnerConcrete(123)
val s = serialize(bippy)
// time passes...
val obj = deserialize[OuterObject.InnerConcrete](s)
Run Code Online (Sandbox Code Playgroud)
所以,我可以找到ScalaSig for OuterTrait,但我还没有找到一个很好的通用方法来识别InnerConcrete类中的外部对象.任何原型?
我在C#中使用XmlSerializer遇到了一些令人惊讶的行为.考虑以下代码.
public class A : IEnumerable
{
public class B
{
[XmlAttribute]
public string PropA { get; set; }
[XmlElement]
public string PropB { get; set; }
}
public IEnumerator GetEnumerator ()
{
yield break;
}
}
class Program
{
static void Main (string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(A.B));
XmlTextWriter writer = new XmlTextWriter(@"E:\temp\test.xml", Encoding.Default);
serializer.Serialize(writer, new A.B() { PropA = "one", PropB = "two" });
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我尝试序列化嵌套类AB的实例,它本身不以任何方式使用容器类A. 但是当我尝试为它构造XmlSerializer时,抛出以下异常:
InvalidOperationException未处理:
要成为XML可序列化,从IEnumerable继承的类型必须在其继承层次结构的所有级别都具有Add(System.Object)的实现.Test.A没有实现Add(System.Object).
当我实际尝试序列化类型AB时,XmlSerializer正在尝试对类型A应用序列化约束我的理解是,除了在外部类型的实例中对数据的特权访问之外,嵌套类型不是特殊的并且表现得好像它是在命名空间中.
这种理解是不正确的,并且嵌套类型或XmlSerializer的语义是否证明了这种行为,或者这是否像XmlSerializer中的错误一样?
特别是对于XmlSerializer语义,是否有任何文档要求在针对嵌套类型应用时对所有外部类型强制执行XmlSerializer约束?
为什么A中的专业化S和B中的S不是?
(如果B未被注释掉)GCC 4.8.1:错误:非命名空间范围'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 > {}; …Run Code Online (Sandbox Code Playgroud) 说我有这些课程:
class Base
{
public:
class Foo { ... };
...
};
Run Code Online (Sandbox Code Playgroud)
然后另一个类派生自基数:
class Derived : public Base
{
// no mention or redefinition of nested class "Foo" anywhere in "Derived"
};
Run Code Online (Sandbox Code Playgroud)
这是否意味着我们现在有一个独特的Derived::Foo,或者Derived::Foo完全相同Base::Foo?
以下是这种情况的一个转折:如果有人抛出一个实例Derived::Foo怎么办?它会在这种情况下被捕获:
catch ( const Base::Foo &ex )
{
// would this also catch an instance of Derived::Foo?
}
Run Code Online (Sandbox Code Playgroud) nested-class ×10
c++ ×4
c# ×2
reflection ×2
scala ×2
templates ×2
.net ×1
c++11 ×1
constructor ×1
inheritance ×1
java ×1
python ×1
scope ×1
try-catch ×1
types ×1
visibility ×1