Advertisement

Nested Class wierdness

Started by March 30, 2001 01:33 PM
3 comments, last by Mithrandir 23 years, 10 months ago
okay, so i''m finally playing around with nested classes and my template library, and since nested classes are not even mentioned in any of my C++ books, i''m just playing around seeing how they work. So, i made a linked list, with a nested protected node class inside it. The data in the node is public, since we know that the linked list is the only class who can even access the class. then I made a public accessor function for the linked list, which returns a pointer to a node... I was expecting this to cause a comiler error, returning a pointer to a protected class in a public function... but it compiled. so I actually used the function to retrieve a node, and modify one of the node''s data members... and it worked! what the hell?! lol, this is kinda funny. it looked somewhat like this (i removed the template stuff just to show basic concepts):
  
class linkedlist
{
protected:
    class node
    {
    public:
        int m_data;
        node* next;
        node* previous;
    };
    
public:
    node* getNode();
};

int main()
{
    linkedlist temp;
    // node* blah;  <--- causes compiler error, as expexted.


    temp.getNode()->m_data = 5;
}

  
that last line compiles and executes perfectly on my system, heh. is this just an oversight in VC, or what? I think i need to know a lot more about nested classes, anyone know of anyplace to find info? This seems to be an incredibly difficult subject to find help on. =============================================== Hurry up madness, hurry up disease, hurry up insanity, hurry up please. Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
by the way, actually knowing that the code i posted above compiles gives me a profound sense of wrongness.

===============================================
Hurry up madness, hurry up disease,
hurry up insanity, hurry up please.
Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Advertisement
You are correct sir. Although your first test wasn''t correct as you''ve written it (copy/paste error?); it should be:
linkedlist::node* blah; // specifically give scope to nested class

This gives the expected compiler error:
C:\projects\test\test.cpp(21) : error C2248: ''node'' : cannot access protected class declared in class ''linkedlist''        C:\projects\test\test.cpp(5) : see declaration of ''node'' 

However, the rest of the program compiles fine, including the bizarre line in question. This is also disturbing to me. SS 9.7 of the ARM discusses nested classes, but gives no discussion of the affects of permissions.

Any compiler lawyers out there want to take a stab at this?
yeah, i just did a quick post of a class similar to my actual code... the real classes are much more complicated, heh.

i just wanted to show the jist of the code.

===============================================
Hurry up madness, hurry up disease,
hurry up insanity, hurry up please.
Hooray! I say, for the end of the world.
This is my signature. There are many like it, but this one is mine. My signature is my best friend. It is my life. I must master it as I must master my life. My signature, without me, is useless. Without my signature, I am useless.
Here''s my scoop:

You create a class called node that only has visibility within the the class linkedlist.
Class node is created as an unnamed variable within linkedlist.
So it is a variable as well as a class.

You can use the function getnode, because it is a function of linkedlist and linkedlist knows about the class node. It is not main that knows about node. It only knows linkedlist and it''s function get node.

Because you do not assign the value to anything, it does not do anything with the return value.
You cannot ever use the return value of getnode, unless you use another linkedlist class function with it.

You could create a void pointer, to contain the address. But you still could not use it within main for anything useful.

My suggestion is this:
Either declare the class node outside of linkedlist, so the class can be referenced outside of linkedlist, then declare a variable of type node within linkedlist.

Or Make it a public.

Or keep it private, for use only by variables of type linkedlist.

This topic is closed to new replies.

Advertisement