标签: nested-class

从通用外部类返回指向嵌套内部类的指针

我是C++的新手,所以请耐心等待.我有一个名为A的泛型类.A有一个名为B的嵌套类.A包含一个名为getB()的方法,它应该返回一个B的新实例.但是,我无法获取我的代码进行编译.这是它的样子:#include

template <class E>
class A {

public:
    class B {
    public:
        int data;
    };

    B * getB(); 
};
Run Code Online (Sandbox Code Playgroud)

A.cpp

#include "A.h"

template <class E>
A<E>::B * A::getB() {
    return new B();
}
Run Code Online (Sandbox Code Playgroud)

当我尝试编译它时,我收到以下错误:

error: expected constructor, destructor, or type conversion before '*' token
Run Code Online (Sandbox Code Playgroud)

有人知道我做错了什么吗?

谢谢,

helixed

更新:

感谢大家的快速回复.让这个工作起来我还有点麻烦.在采取此处列出的建议后,我有这样的事情:

template <class E>
class A {

public:
    class B {
    public:
        int data;
    };

    B * getB(); 
};

template <class E>
typename A<E>::B * A<E>::getB() {
    return new B(); …
Run Code Online (Sandbox Code Playgroud)

c++ templates nested-class

6
推荐指数
1
解决办法
2774
查看次数

将嵌套类放在单独的文件中

我有一个嵌套类的文件,但它变得足够长,不可读.有没有办法可以将嵌套类分解为单独的文件?

java nested-class

6
推荐指数
1
解决办法
3593
查看次数

具有嵌套类的Lambda

我刚刚发布了这个问题但得到了我的问题的部分答案,所以我想我发布了更多解释,希望得到更准确的答案.我有2个班:

public class Employee
{
    public string Name { get; set; }
    public List<Cars> Cars { get; set; }
}

public class Car
{
    public int CarID { get; set; }
    public CarTypes CarType { get; set; }
    public enum CarTypes
    {
        Van,
        SmallCar
    }
}
Run Code Online (Sandbox Code Playgroud)

我试图只获得所有已经分配了使用Lambda忽略那些使用SmallCars的员工的员工,我尝试了这一行:

List<Employee> EmployeesWithVans = AllEmployees.Where(emps => emps.Car.Any(cartype => cartype.CarType == Car.CarTypes.Van)).ToList();
Run Code Online (Sandbox Code Playgroud)

但是,如果至少有一辆面包车被分配给一个员工(.Any),如果我尝试(.All)它会带回任何东西,因为并非所有员工都有范.

是否可以使用嵌套的Lambda实现这一点?

谢谢.

编辑:

Employee Mark = new Employee();
Mark.Cars.Add(new Car() { CarType = Car.CarTypes.Van, CarID = 12 }); …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda .net-4.0 nested-class

6
推荐指数
1
解决办法
3353
查看次数

在嵌套类中使用c#泛型

考虑以下类结构:

public class Foo<T>
{
    public virtual void DoSomething()
    {
    }

    public class Bar<U> where U : Foo<T>, new()
    {
        public void Test()
        {
            var blah = new U();
            blah.DoSomething();
        }
    }
}

public class Baz
{
}

public class FooBaz : Foo<Baz>
{
    public override void DoSomething()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

当我去使用嵌套类时,我有以下内容:

var x = new FooBaz.Bar<FooBaz>();
Run Code Online (Sandbox Code Playgroud)

必须两次指定它似乎是多余的.我如何创建我的类结构,以便我可以这样做:

var x = new FooBaz.Bar();
Run Code Online (Sandbox Code Playgroud)

在嵌套类的where子句中是否应该有一些方法来说U总是父类?怎么样?


更新:添加了上面的DoSomething()方法来解决一些注释.重要的是,当我调用DoSomething时,它会解决被覆盖的版本.如果我只使用Foo而不是U,则会调用基本实现.

c# generics nested-class

6
推荐指数
1
解决办法
1886
查看次数

Python中的嵌套类

令人惊讶的是, 在Python中处理类(嵌套等)看起来并不容易!最近我出现了以下问题并花了几个小时(尝试,搜索......)但没有成功.我阅读了大部分SO相关链接,但没有一个指出这里提出的问题!

#------------------------------------
class A:
    def __init__(self):
        self.a = 'a'
        print self.a

class B(A):
    def __init__(self):
        self.b = 'b'
        A.a = 'a_b'
        print self.b, A.a
#------------------------------------
class C:
    class A:
        def __init__(self):
            self.a = 'a'
            print self.a

    class B(A):
        def __init__(self):
            self.b = 'b'
            A.a = 'a_b'
            print self.b, A.a
#------------------------------------
#------------------------------------
>>> c1 = A()
a
>>> c1.a
'a'
>>> c2 = B()
b 
>>> c2.a, c2.b
('a_b', 'b')
>>> c3 = C()
>>> c4 = c3.A()
a
>>> c4.a
'a'
>>> c5 = …

python class nested-class

6
推荐指数
1
解决办法
2万
查看次数

接口内的嵌套类

java编译器允许我在接口内编写一个Class定义.这有什么具体用途吗?

interface ClassInterface {

  void returnSomething();
  int x = 10;

  class SomeClass {
    private int y;

    private void classDoingSomething() {         
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

请解释 .

java interface nested-class

6
推荐指数
1
解决办法
3126
查看次数

linq嵌套列表包含

我有一个问题,你在那里linq专家!在组件实例的嵌套列表中,我需要知道其中是否存在特定类型的组件.可以用linq表达吗?考虑到可能有application.Components [0] .Components [0] .Components [0] ...我的问题是面向linq中的递归查询!

我告诉你实体让你对模型有所了解.

public class Application
{
    public List<Component> Components { get; set; }
}

public class Component
{
    public ComponentType Type { get; set; }
    public List<Component> Components { get; set; }
}

public enum ComponentType
{
    WindowsService,
    WebApplication,
    WebService,
    ComponentGroup
}
Run Code Online (Sandbox Code Playgroud)

c# linq tree recursion nested-class

6
推荐指数
1
解决办法
2965
查看次数

使用声明和访问修饰符的嵌套类

编译一些代码,我遇到了编译器错误,这对我来说似乎很奇怪,并且与继承,嵌套类,使用声明和访问修饰符有关.

基本上,目的是使派生类型公开基类型的嵌套受保护类.

以下简短示例来说明问题:

class Base
{
protected:

    struct Nested
    {
        enum value_enum
        {
            val = 0,
            val2,
            val3
        };
    };

};

class Derived : public Base
{
public:

    using Base::Nested;
};


int main(int argc, char** argv)
{
    //Base::Nested aa; // error, as (I) expected
    //Base::Nested::value_enum ab; // error, as (I) expected

    Derived::Nested ba; // works, as (I) expected
    Derived::Nested::value_enum bb; // MSVC error, as (I) did not expect


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

看到现场.

MSVC11(v11.00.61030)对此代码进行了阻塞,并出现以下错误:

错误C2248:'Base :: Nested':无法访问类'Base'中声明的受保护结构

GCC和Clang都正确地编译了这个,因此如果没有引用标准中相关部分的能力,我认为这是一个MSVC错误.

有没有办法解决这个与MSVC?

c++ access-modifiers nested-class compiler-bug visual-studio-2012

6
推荐指数
1
解决办法
425
查看次数

java中'new'的语法

非静态成员类的构造函数采用额外的隐藏参数,该参数是对直接封闭类的实例的引用.还有一个'new'的语法扩展.

在下面的代码中,

class K{
    static class Ka{
        static class Kb{
            class Kc{
                class Kd{

                }
            }
        }
    }
}

class Test{
    K.Ka.Kb.Kc.Kd k = new K.Ka.Kb().new Kc().new Kd();
}
Run Code Online (Sandbox Code Playgroud)

能否请您帮助我理解的意思Kb()K.Ka.Kb().new Kc().new Kd()?据我所知,这new Kc()是第一段所述.

java nested-class inner-classes

6
推荐指数
2
解决办法
332
查看次数

使用嵌套类的奇怪enable_if行为(MSVC编译器错误或功能?)

经过一段时间的调试后,我使用enable_if跟踪了出现问题的原因以及一些意外的模板专业化结果:

以下代码使Visual Studio 2010(和2008)在DoTest()中的声明失败,而在g ++ 3.4.5中则没有。但是,当我从SomeClass中删除模板或将my_condition移出SomeClass的范围时,它也可以在MSVC中使用。

这段代码是否有问题(至少部分地)解释了此行为,或者这是MSVC编译器中的错误?

(使用此示例代码,对于boost和c ++ 0x stl版本是相同的)

#include <cassert>
#include <boost\utility\enable_if.hpp>

template <class X>
class SomeClass {
public:
    template <class T>
    struct my_condition {
        static const bool value = true;
    };

    template <class T, class Enable = void> 
    struct enable_if_tester { 
        bool operator()() { return false; }
    };

    template <class T>
    struct enable_if_tester<T, typename boost::enable_if< my_condition<T> >::type> { 
        bool operator()() { return true; }
    };

    template <class T>
    void DoTest() …
Run Code Online (Sandbox Code Playgroud)

c++ templates nested-class visual-studio enable-if

5
推荐指数
1
解决办法
577
查看次数