Jump to content
  • Advertisement

Archived

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

kiehl

stupid c++ question, but answear needed

This topic is 5623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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

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

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!