标签: nested-class

C++私有嵌套抽象类

所以也许这是一个愚蠢的问题,我在想这个,但我有以下情况.我正在制作一个"类Shell",它可以运行抽象的"类Action"对象.它是唯一应该创建或使用这些对象的类.操作对象需要访问Shell以对其执行特定操作,但我试图避免为此添加公共接口(不应该允许其他人这样做).

我原来有一个简单的(不那么优雅)

class Shell
{
 public:
    bool checkThing();
    // etc...
 private:
    bool _thing;
};

class Action
{
 public:
    virtual void execute( Shell &s )=0;
};

class ChangeAction : public Action
{
 public:
    void execute( Shell &s )
    {
        // requires friendship or public mutator!
        s._thing = true;
    }
};
Run Code Online (Sandbox Code Playgroud)

所以我考虑了一个嵌套类Action,但是我想把它变成私有的(为什么让其他人做除了Shell之外的具体动作,对吧?)

class Shell
{
 public:
    bool checkThing();
    // etc...
 private:
    bool _thing;
    class Action;
};

class Shell::Action
{
 public:
    virtual void execute( Shell &s )=0;
};

class ChangeAction : public Shell::Action …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance friend nested-class

7
推荐指数
1
解决办法
1626
查看次数

内部类访问

用另一个类(内部类)的方法编写的类可以访问方法变量吗?我的意思是在下面的代码中:

class A
{
  void methodA( int a )
  {
    class B
    {
      void processA()
      {
         a++;
      }
    };
     std::cout<<"Does this program compile?";
     std::cout<<"Does the value of the variable 'a' increments?";
  };
Run Code Online (Sandbox Code Playgroud)

};

这合法吗? 'a' 的值会增加吗?请建议如何。

谢谢,帕万。

c++ nested-class

7
推荐指数
1
解决办法
4701
查看次数

在Python中覆盖嵌套类成员的更好方法是什么?

我需要"覆盖"一些基类的嵌套类成员,同时保持其余的完整.
这就是我做的:

class InternGenericForm(ModelForm):                
    class Meta:
        model = Intern
        exclude = ('last_achievement', 'program',)
        widgets = {
            'name': TextInput(attrs={'placeholder': '??? ? ???????' }),
        }

class InternApplicationForm(InternGenericForm):
    class Meta:
        # Boilerplate code that violates DRY
        model = InternGenericForm.Meta.model
        exclude = ('is_active',) + InternGenericForm.Meta.exclude
        widgets = InternGenericForm.Meta.widgets
Run Code Online (Sandbox Code Playgroud)

其实,我想InternApplicationForm.Meta完全相同一样InternGenericForm.Meta,不同之处在于它的exclude元组应该包含一个以上的项目.

在Python中这样做的更美妙的方法是什么?
我希望我不必编写样板代码model = InternGenericForm.Meta.model,因为它也容易出错.

python syntax inheritance class nested-class

7
推荐指数
1
解决办法
1056
查看次数

访问内部类中外部类的私有成员数据

有这个代码:

#include <iostream>

class Outer{
    int a; // private data member of class Outer
public:
    Outer(): a(55){}
    class Inner{
    public:
        void fun(Outer ob){
            std::cout << ob.a << std::endl;
        }
    };
};

int main() {

    Outer::Inner object;
    object.fun(Outer()); // prints 55
    //std::cout << (Outer().a) << std::endl; error: 'int Outer::a' is private

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

为什么内部类可以访问类外部的私有成员数据'a'?在本文XL C/C++ V8.0 for Linux之后,它不应该编译,但它在g ++ 4.4.0上编译.

c++ nested-class

7
推荐指数
2
解决办法
2853
查看次数

嵌套类.GetType()

在弄乱嵌套类并将类型名称输出到控制台窗口时,我注意到了一些好奇.我想知道是否有人可以为我解释一下.当调用GetType()主类时,它返回我期望的内容,即相关名称空间后的类的名称.即Namespace.Namespace.Classname

但是,当我从封闭类中调用一个函数来返回嵌套类的类型时,我得到返回的值,如下所示:

Namespace.Namespace.ClassNameEnclosing + ClassNameNested.
Run Code Online (Sandbox Code Playgroud)

为什么它不是简单地以点符号返回.为什么+符号?我只是好奇在后台发生了什么导致这种表示法.

c# types nested-class

7
推荐指数
1
解决办法
724
查看次数

Java中的兄弟嵌套类可以访问彼此的私有成员

我发现java中的两个嵌套类可以访问彼此的私有成员.为什么会这样?它是一个错误还是标准指定的?

以下代码编译并运行,没有错误.

public class Main {
public static void main(String args[]) {
    A a = new A();
    a.var1 = 12;
    B b = new B();

    System.out.println(a.var1);
    b.printA(a);
}

private static class A {
    private int var1;
}

private static class B {
    private int var2;

    public void printA(A a) {
         // B accesses A's private variable
         System.out.println(a.var1);
    }

}

}
Run Code Online (Sandbox Code Playgroud)

java nested-class private-members

7
推荐指数
1
解决办法
1254
查看次数

允许访问但阻止外部类实例化嵌套类

我想要定义容器类和外部类可访问的嵌套类,但我想控制嵌套类的实例化,这样只有容器类的实例才能创建嵌套类的新实例.

诉讼代码应该有希望证明这一点:

public class Container
{
    public class Nested
    {
        public Nested() { }
    }

    public Nested CreateNested()
    {
        return new Nested();  // Allow
    }
}

class External
{
    static void Main(string[] args)
    {
        Container containerObj = new Container();
        Container.Nested nestedObj;

        nestedObj = new Container.Nested();       // Prevent
        nestedObj = containerObj.CreateNested();  // Allow

    }
}
Run Code Online (Sandbox Code Playgroud)

Nested必须是公开的,以便可以访问它External.我尝试使用Nested受保护的构造函数,但是这会阻止Container创建实例,因为Container它不是基类Nested.我可以将构造函数设置为Nestedto internal,但我希望阻止所有外部类(包括同一程序集中的那些外部类)访问构造函数.有没有办法做到这一点?

如果这不能通过访问修饰符实现,我想知道我是否可以在其中抛出异常Nested().但是,我不知道如何测试new Nested()调用的上下文.

c# encapsulation nested-class inner-classes

7
推荐指数
1
解决办法
1228
查看次数

多重继承隐藏的嵌套类

这段代码是否有效C++(11)?

struct Base {
    template <typename>
    struct nested;
};
struct Derived1 : Base { };
struct Derived2 : Base { };
struct Derived3 : Derived1, Derived2 { };

typedef Derived3::nested<int> xxx;
Run Code Online (Sandbox Code Playgroud)

我知道的

上面的代码无法编译:

  • Apple LLVM 5.0(clang-500.2.75)
  • Clang 3.4

但它成功编译:

  • gcc 4.9.0 20131110(实验性)
  • gcc 4.8

此外,如果我将nested类型更改为非模板类​​型,即

struct Base {
    struct nested;
};
...
typedef Derived3::nested xxx;
Run Code Online (Sandbox Code Playgroud)

然后它适用于上述编译器.

[edit]nested模板结构更改为模板别名也不会改变任何内容;

template <typename> struct dependent { struct type; };
struct Base {
    template <typename T>
    using nested = typename …
Run Code Online (Sandbox Code Playgroud)

c++ gcc multiple-inheritance clang nested-class

7
推荐指数
1
解决办法
322
查看次数

C++调用嵌套模板类析构函数

假设我有一个类型为std :: vector :: iterator的变量x,它被分配(出于其他原因)并且放置为new,例如

new((void*)&x) std::vector<int>::iterator();
Run Code Online (Sandbox Code Playgroud)

如何以符合标准的方式调用析构函数?干

x.std::vector<int>::iterator::~iterator();
Run Code Online (Sandbox Code Playgroud)

例如在gcc中工作但不是clang.

c++ templates destructor nested-class

7
推荐指数
1
解决办法
532
查看次数

"使用typename"指令是否未由编译器实现?

我有一些看起来像这样的代码:

namespace myLibrary
{
    class A
    {
    public:
        struct Nested
        {
            ...
        };

        ...
    };
}
Run Code Online (Sandbox Code Playgroud)

在代码的其他部分,我需要访问A.因为我喜欢可读的代码,我也喜欢using指令:

using myLibrary::A;
...
A a;
Run Code Online (Sandbox Code Playgroud)

现在,在某些时候我还需要访问我的嵌套类,所以我想写这样的东西:

using myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)

显然,编译器不能知道这是一个嵌套类而不是类成员,并给我一个错误:

error : using declaration can not refer to class member
Run Code Online (Sandbox Code Playgroud)

我无法理解的是为什么这不能解决问题:

using typename myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)

编译器仍然给我完全相同的错误!

幸运的是,我有其他选择:

// Using a typedef
typedef myLibrary::A::Nested Nested;

// Using the new C++11 syntax for type aliasing
using Nested = myLibrary::A::Nested;
Run Code Online (Sandbox Code Playgroud)

但我想了解为什么使用typename指令不起作用.它不符合我的想法吗?或者它不是由编译器实现的?如果是后者,是否有理由呢?

c++ using nested-class typename

7
推荐指数
1
解决办法
3747
查看次数