Thread: Nested Classes

  1. #1
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Nested Classes[edit --wrong terminology] I mean nested Class Objects

    I haven't been able to find an answer to this. When you declare a class instance inside another class it has access to all that classes member variables. Now if you have functions for the nested class that work on these member variables you don't have to pass them through the parameter list for the function right? But if you don't pass them through the parameter list then this nested class is no longer discrete. Any changes you make will disrupt both classes. And you can't really use that class as a stand alone class. I would think that if the nested class acts on the variables directly it would be faster then if it had to have the variables passed through the parameter list. Does anyone know if is is in fact faster or does the compiler optimize this away so it really makes no difference or the difference would be negligable.

    [edit]
    Sorry for the mixup i was reading another webiste talking about nesting a class declaration within another class.
    Last edited by manofsteel972; 11-21-2004 at 12:26 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    The speed will be the same. It still has to access it in memory. A nested class isn't written to be a stand alone class. Its written to be a part of the other class.

  3. #3
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I was just playing around with my BlackJack game and decided to make it out of nested Classes as an experiment. Not really the best use of nested classes. I have a deck class a player class a display class and a main BlackJack class each inside the other and I was just noticeing how if I wrote the member functios to use the member variables instead of passing by parameter how it makes it impossible to make changes in any one class without dirupting the others. If there is no advantage to using the member variables directly then I could just pass by parameter list and still maintain modularity and make it easy to make changes.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    From msdn:

    Quote:

    Nesting a class within another class does not give special access privileges to member functions of the nested class. Similarly, member functions of the enclosing class have no special access to members of the nested class.

    End Quote.
    Therefore, if you want any changes made in the outer class members manipulated by the nested class members you will probably need to pass them by reference, use friend functions, or make the classes friends of each other.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I wasn't referring to member functions but to member variables. ie I declare a class with a member variable
    Code:
    class Myclass
    {
    int myvariable;
    NestedClass Nestor;
    };
     
    Nestor::Function()
    {
    myvariable=21; //I have access to the member variable without 
    } //without passing the variable through a parameter list; this ties this 
    //function to this variable name meaning if i changed it i would have to 
    //come back here and modify this function
    [edit]
    Nevermind I got the terminology wrong. I don't think I am dealing with a nested class. Just nested objects.
    Last edited by manofsteel972; 11-21-2004 at 12:17 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone with a spare java class file(s) with nested classes?
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 04-29-2009, 08:33 PM
  2. Problem with nested classes
    By Bargi in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 05:52 AM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM
  5. nested classes...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-09-2001, 04:25 PM