stupid c++ question, but answear needed

Started by
9 comments, last by kiehl 20 years, 11 months ago
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]
my english sucks :(
Advertisement
You can''t declare a class inside another class.

Do it like this:

class CInit
{
public:
void all();
};

class CApp
{
public:
CInit init;
};

now you can do this

Capp app;
app.init.all();

My Site
thxxx

it looks so obvious now
my english sucks :(
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 ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
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]
I never knew this. Are there some rules for when to use this?

My Site
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.
Lektrix:

thx, I think i''ll use ur method



my english sucks
my english sucks :(
Wait, please forgive me if this was already said but what is the use for sub-classes?

There''s no town drunk here, we all take turns.
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.

This topic is closed to new replies.

Advertisement