Archived

This topic is now archived and is closed to further replies.

kiehl

stupid c++ question, but answear needed

Recommended Posts

I want to make a submember(?) in a class, i tried this, but it doesnt work
    

class Capp{
            public:
            
            Capp(){};
            ~Capp(){};
            
            
            class init{
            
                        public:
                        
                        init(){};
                        ~init(){};
                        int video();
                        int opengl();
                        int input();
                        int audio();
                        int console();
                        int all();
                        
                        
                        private:
                        
                        };
            
            
            
            private:
            
};

  
      

Capp app;
app.init.all(); // this doesn't work... why???


    
any idea how to do it??? do I have to use derived classes?? how?? thx [edited by - kiehl on May 3, 2003 4:33:10 PM]

Share this post


Link to post
Share on other sites
quasar3d: Yes, you can declare a class inside a class. See my code sample below; by placing the CInit declaration inside the Capp class, you give it it class scope. So, for example, you can''t use it outside of Capp.

kiehl:

You need to make an object/instance of type init. Remember, class declarations/definitions merely describe how memory is to allocated, etc. They don''t actually create an object. I think this is what you want to be doing:


class Capp
{
private:
    class CInit
    {
    public:
        init(){};
        ~init(){};
        int video();
        int opengl();
        int input();
        int audio();
        int console();
        int all();
    private:

    };

public:
    Capp(){};
    ~Capp(){};
    CInit init;

private:

};


Now you can do:

Capp app;
// ...
app.init.all();


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

Share this post


Link to post
Share on other sites
i think you can declare a class within a class. your problem is that you're using "app.init.all();" incorrectly. You need CApp to have an instance of init to use it. So you'd have a member in CApp called init m_Init; and then you can use that instance to access the members of your init class.

correct me if i'm wrong.

EDIT: beaten... too slow haha

[edited by - knyot on May 3, 2003 4:54:06 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by quasar3d
I never knew this. Are there some rules for when to use this?


The classic use is a class that the enclosing class uses as a private member. like so:

  
class List
{
public:
// ...

private:
class Node
{
// ...

} *head, *tail;
};

Another use: look how you declare STL iterators. They are classes defined within the container class:

  
class List
{
public:
class Iterator
{
// ...

};
};




How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
quote:
Original post by Ekim_Gram
Wait, please forgive me if this was already said but what is the use for sub-classes?

Read my last post.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
I agree completely with Sneftel; be aware, however, that in C++, nested classes (subclassing is something very different, related to inheritance) don''t have a special relationship with enclosing classes, beyond being confined to their scope. This is very different from Java, for example, where an inner class has access to the outer class''s private data members and, unless declared static, instances of this are implicitly related to the instance of the outer class from which they were instantiated (which, IMO, is a very nice feature and one of the rather few times where I prefer the way Java handles something).

Share this post


Link to post
Share on other sites