DebrisHauler
DebrisHauler

Reputation: 41

Is there a way you can create an instance of a class that resides within another class?

Is there a way you can create an instance of a class that resides within another class? For example:

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.
    }
}

Now I just want to create an instance of bar in my main() like this:

foo* fire;
bar* tinder;

But bar is not declared in this scope. The reason I am using a class within a class is because they both use methods that take the other class as a parameter, but I need an instance of each class in my main(). What can I do?

Upvotes: 0

Views: 158

Answers (5)

Asdine
Asdine

Reputation: 3046

Nested classes are declared inside the scope of another class. So to use them from main you need to tell the compiler where to find that class.

Here is the syntax :

foo::bar *tinder;

foo being the parent scope and bar the nested class.

Hope it helped

Upvotes: 1

Gio
Gio

Reputation: 3340

What you want is a so called "nested class".

You can find everything you want to know about this here: Why would one use nested classes in C++?

e.g:

class List
{
    public:
        List(): head(NULL), tail(NULL) {}
    private:
        class Node
        {
          public:
              int   data;
              Node* next;
              Node* prev;
        };
    private:
        Node*     head;
        Node*     tail;
};

Upvotes: 0

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

class bar is declared in the scope of class foo. So you have to write

foo::bar* tinder;

Also you forgot to place semicolons after the definitions of class bar and foo.:)

Upvotes: 1

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

You can use the scope resolution operator: foo::bar* tinder;. This will give you a pointer to a bar, not a bar object. If you want that, you should do foo::bar tinder.

However, you don't have a good reason for using a nested class. You should just put one before the other and then use a forward declaration. Something like:

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.
    }
};

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227418

Now I just want to create an instance of bar in my main() ...

This is how you would do that:

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

bar is declared in the scope of foo. It isn't clear why that is, so unless you have a good reason for it, don't use a nested class. Also note that you were trying to declare pointers to foo and foo::bar, not instances.

Upvotes: 2

Related Questions