用外部 class 初始化内部 Class 这可能吗?
Initialize Inner Class with outer class this possible?
今天我试图实例化一个内部 class,同时将我的外部 class 传递给它,同时我在外部 class 的命名空间中:
我正在使用 Visual Studo 2013。
代码看起来像这样:(看^^)
class Base
{
public:
virtual void foo(){ cout << "foo" };
};
class object
{
class Derived : public Base
{
object& o;
public:
Derived(object& o) : o(o){}
virtual void foo(){ cout << "bar" };
}derived(*this);
// ^^^^^^
};
据我测试,派生的 class 继承某些内容不会影响此处示例的任何内容。 (仅出于上下文原因,见下文)
关于这个 ^^ 点我收到错误:
没有合适的默认构造函数可用
Intellisense 警告我,它需要类型说明。
我也尝试传递一个指针(当然我也改变了构造函数)但同样的反应。
对于 protokoll,我现在尝试了很多变体和研究,但我无法为我的问题找到一个明确的答案。
"this" 指针在这里不可用吗?我怎么能在这一点上通过自己呢?
For Background (only if you're interested):
I tried to write Code for Keybinding in an Application. To pass
functions to the Keys i use an "Interface" of class KeyFunction (Base
class resembles it).
I now want to give classes (object) the possibility to declare it's
own KeyFunction(Derived) and , more important, pass ressources(object)
with it, in a way that functions can work on them (since i can only
use void pointers, because they are later stored in an array for the
bindings) I already achieved this task with other code which i think
is to long to post here, though. By experimenting i stumbled across
this problem.
您的编译错误与您的 class 层次结构无关,但有一个简单的事实,即这不是您构建 class 实例的方式。
尝试实际声明一个 class 成员和一个 class 构造函数:
class Base
{
public:
virtual void foo(){ }
};
class object
{
class Derived : public Base
{
object& o;
public:
Derived(object& o) : o(o){}
virtual void foo(){ }
};
Derived derived;
object() : derived(*this)
{
}
};
今天我试图实例化一个内部 class,同时将我的外部 class 传递给它,同时我在外部 class 的命名空间中: 我正在使用 Visual Studo 2013。 代码看起来像这样:(看^^)
class Base
{
public:
virtual void foo(){ cout << "foo" };
};
class object
{
class Derived : public Base
{
object& o;
public:
Derived(object& o) : o(o){}
virtual void foo(){ cout << "bar" };
}derived(*this);
// ^^^^^^
};
据我测试,派生的 class 继承某些内容不会影响此处示例的任何内容。 (仅出于上下文原因,见下文)
关于这个 ^^ 点我收到错误: 没有合适的默认构造函数可用
Intellisense 警告我,它需要类型说明。 我也尝试传递一个指针(当然我也改变了构造函数)但同样的反应。 对于 protokoll,我现在尝试了很多变体和研究,但我无法为我的问题找到一个明确的答案。
"this" 指针在这里不可用吗?我怎么能在这一点上通过自己呢?
For Background (only if you're interested):
I tried to write Code for Keybinding in an Application. To pass functions to the Keys i use an "Interface" of class KeyFunction (Base class resembles it).
I now want to give classes (object) the possibility to declare it's own KeyFunction(Derived) and , more important, pass ressources(object) with it, in a way that functions can work on them (since i can only use void pointers, because they are later stored in an array for the bindings) I already achieved this task with other code which i think is to long to post here, though. By experimenting i stumbled across this problem.
您的编译错误与您的 class 层次结构无关,但有一个简单的事实,即这不是您构建 class 实例的方式。
尝试实际声明一个 class 成员和一个 class 构造函数:
class Base
{
public:
virtual void foo(){ }
};
class object
{
class Derived : public Base
{
object& o;
public:
Derived(object& o) : o(o){}
virtual void foo(){ }
};
Derived derived;
object() : derived(*this)
{
}
};