所以也许这是一个愚蠢的问题,我在想这个,但我有以下情况.我正在制作一个"类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) 用另一个类(内部类)的方法编写的类可以访问方法变量吗?我的意思是在下面的代码中:
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' 的值会增加吗?请建议如何。
谢谢,帕万。
我需要"覆盖"一些基类的嵌套类成员,同时保持其余的完整.
这就是我做的:
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,因为它也容易出错.
有这个代码:
#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上编译.
在弄乱嵌套类并将类型名称输出到控制台窗口时,我注意到了一些好奇.我想知道是否有人可以为我解释一下.当调用GetType()主类时,它返回我期望的内容,即相关名称空间后的类的名称.即Namespace.Namespace.Classname
但是,当我从封闭类中调用一个函数来返回嵌套类的类型时,我得到返回的值,如下所示:
Namespace.Namespace.ClassNameEnclosing + ClassNameNested.
Run Code Online (Sandbox Code Playgroud)
为什么它不是简单地以点符号返回.为什么+符号?我只是好奇在后台发生了什么导致这种表示法.
我发现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) 我想要定义容器类和外部类可访问的嵌套类,但我想控制嵌套类的实例化,这样只有容器类的实例才能创建嵌套类的新实例.
诉讼代码应该有希望证明这一点:
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++(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)
我知道的
上面的代码无法编译:
但它成功编译:
此外,如果我将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) 假设我有一个类型为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.
我有一些看起来像这样的代码:
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指令不起作用.它不符合我的想法吗?或者它不是由编译器实现的?如果是后者,是否有理由呢?