views:

78

answers:

2

we make a class with data member and its methods.. now my question is that can we make a classes inside a class.. i think we can get it by using template.. like

template <class T> 
class class_template_list
{
public:
 class class_link 
 {
 public:
  T  item;
  class_link* prev;
  class_link* next;
  class_link(const T& value)  { prev = next = this; }
 };

now here I use class inside a class of class_template_lsist where I already defined a template of class and make and an object like T and my class is class_template_list.Am I right? And tell me please that what is this here.

A: 

It appears, from the snippet you posted, that you're creating something like a linked list. In that case, yes, it is possible to have a list containing a class, and the way to do that is with templates. I'm not entirely sure why you need a class within a class like that (with no other members/methods), but if that's required by your system (and legal, which I'm not entirely sure about), then it might make sense with more context.

In the spot you used this, it would be a class_link *, or perhaps a class_template_list<T>::class_link * or just class_link<T> *. Again, it depends on if you need and want that class-in-class bit there.

It's also entirely possible I misinterpreted the whole question, since it's pretty vague and confusing. If you can edit it to be clearer, I'll edit my answer.

peachykeen
A: 

You can define a class inside another class, but it doesn't make an instance of the nested class aware of an instance of the outer class.

It does, in practice, confer access. But the rules for that have changed at least two times, and I think it's three times. Here's an example:

#include <iostream>
using namespace std;

class Outer
{
private:
    int     blah_;

    class Inner
    {
    public:
        void foo()
        {
            cout << Outer().blah_ << endl;
        }
    };

public:
    Outer(): blah_( 42 ) {}
    void bar() { Inner().foo(); }
};

int main()
{
    Outer().bar();
}

This compiles nicely with MinGW g++ 4.4.1, Visual C++ 10.0, and Comeau Online 4.3.10.1. It's as if the nested class is automatically a friend of the outer class. But again, the rules for that have been in flux, and are perhaps still in flux, I don't know.

Expressing the same without using a nested class definition would make the nested class visible to client code.

But it illustrates what goes on, what the code above means:

#include <iostream>
using namespace std;

class Inner;

class Outer
{
friend class Inner;
private:
    int     blah_;

public:
    Outer();
    void bar();
};

class Inner
{
public:
    void foo();
};

Outer::Outer(): blah_( 42 ) {}
void Outer::bar() { Inner().foo(); }

void Inner::foo()
{
    cout << Outer().blah_ << endl;
}

int main()
{
    Outer().bar();
}

By the way, as you can see nesting class definitions has nothing to do with templating. Commonly we just say that such logically unrelated features are "orthogonal", meaning that they generally don't interact.

Cheers & hth.,

Alf P. Steinbach
hy please i am new one in this feild. and i have to read this code and
piyapiya
i come to understand with template like this is combining the classes.. if its looks like this that you said than what s template, its use and why need in which case we should use template..
piyapiya
Alf P. Steinbach