Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 有没有一种方法可以创建驻留在另一个类中的类的实例?_C++ - Fatal编程技术网

C++ 有没有一种方法可以创建驻留在另一个类中的类的实例?

C++ 有没有一种方法可以创建驻留在另一个类中的类的实例?,c++,C++,有没有一种方法可以创建驻留在另一个类中的类的实例? 例如: class foo { public: foo() { //Constructor stuff here. } class bar { bar() { //Constructor stuff here. } void action(foo* a) { //Code t

有没有一种方法可以创建驻留在另一个类中的类的实例? 例如:

class foo
{
public:
    foo()
    {
        //Constructor stuff here.
    }

    class bar
    {
       bar()
       {
           //Constructor stuff here.
       }
       void action(foo* a)
       {
           //Code that does stuff with a.
       }
    }

    void action(bar* b)
    {
        //Code that does stuff with b.
    }
}
现在我只想在main()中创建一个bar实例,如下所示:

foo* fire;
bar* tinder;
但bar不在此范围内声明。我在一个类中使用一个类的原因是,它们都使用将另一个类作为参数的方法,但我需要在main()中每个类的一个实例。我能做什么

现在我只想在main()中创建一个bar的实例

这是您将如何做到的:

int main()
{
  foo::bar tinder;
}
bar
foo
的范围内声明。原因不清楚,所以除非有充分的理由,否则不要使用嵌套类。还要注意,您试图声明指向
foo
foo::bar
的指针,而不是实例

现在我只想在main()中创建一个bar的实例

这是您将如何做到的:

int main()
{
  foo::bar tinder;
}

bar
foo
的范围内声明。原因不清楚,所以除非有充分的理由,否则不要使用嵌套类。还要注意,您试图声明指向
foo
foo::bar
的指针,而不是实例。

您可以使用范围解析操作符:
foo::bar*tinder。这将为您提供指向
的指针,而不是
对象。如果你想这样做,你应该做
foo::bar tinder

但是,使用嵌套类的理由并不充分。您应该将一个放在另一个之前,然后使用前向声明。比如:

class foo; // Forward declares the class foo

class bar
{
   bar()
   {
       //Constructor stuff here.
   }
   void action(foo* a)
   {
       //Code that does stuff with a.
   }
};

class foo
{
public:
    foo()
    {
        //Constructor stuff here.
    }

    void action(bar* b)
    {
        //Code that does stuff with b.
    }
};

您可以使用范围解析操作符:
foo::bar*tinder。这将为您提供指向
的指针,而不是
对象。如果你想这样做,你应该做
foo::bar tinder

但是,使用嵌套类的理由并不充分。您应该将一个放在另一个之前,然后使用前向声明。比如:

class foo; // Forward declares the class foo

class bar
{
   bar()
   {
       //Constructor stuff here.
   }
   void action(foo* a)
   {
       //Code that does stuff with a.
   }
};

class foo
{
public:
    foo()
    {
        //Constructor stuff here.
    }

    void action(bar* b)
    {
        //Code that does stuff with b.
    }
};

类bar在类foo的作用域中声明。所以你必须写作

foo::bar* tinder;

此外,您还忘了在类bar和foo的定义后面加分号。

类bar是在类foo的范围内声明的。所以你必须写作

foo::bar* tinder;

此外,您还忘记在类bar和foo的定义后面加分号。

嵌套类是在另一个类的范围内声明的。 因此,要从main使用它们,您需要告诉编译器在哪里可以找到该类

以下是语法:

foo::bar *tinder;
foo
是父作用域,而
bar
是嵌套类


希望对您有所帮助

嵌套类是在另一个类的范围内声明的。 因此,要从main使用它们,您需要告诉编译器在哪里可以找到该类

以下是语法:

foo::bar *tinder;
foo
是父作用域,而
bar
是嵌套类


希望它有帮助

您需要的是所谓的“嵌套类”

你可以在这里找到你想知道的一切:

e、 g:


您需要的是所谓的“嵌套类”

你可以在这里找到你想知道的一切:

e、 g:


错过了类定义后的分号。错过了类定义后的分号。太棒了。你刚刚教了我一些新的有用的东西。注意,在定义foo之前,你不能用a做任何事情。而是在foo之后定义该成员<代码>类bar{public:bar(){…}内联无效操作(foo*a);}之后:
void bar::action(foo*a){…}
这回答了我的下一个问题。太棒了。你刚刚教了我一些新的有用的东西。注意,在定义foo之前,你不能用a做任何事情。而是在foo之后定义该成员<代码>类bar{public:bar(){…}内联无效操作(foo*a);}之后:
void bar::action(foo*a){…}
这回答了我的下一个问题。我已经在使用嵌套类,这是我问题的一部分。我已经在使用嵌套类,这也是我问题的一部分。