Hi everyone,

I'm trying to do my assignment, and in there, I'm supposed to use a nested class with which seems I'm facing a lot of difficulties. To make things easy to understand and debug, I made a small piece of code as follows:

class outter {
	public:
		int getID();
		void setID(int ID);
	private:
		int ID;
		
		class inner {
			
			public:
				string name;
		};
};

The question is, how can I access from outside to the member variable "name".

Thanks a lot in advance.

Recommended Answers

All 8 Replies

make inner public. Then in you can access it by the scope resolution
operator like so outter::inner variable name;

[edit] The above assumes "outside " means from outside of the class.

are you trying to do inheritance? If you are just declare your "nested" class with

class inner::public outter {

Thanks a lot for your answers. I think to make things clearer, I'd better put here the exact version of ADT we are asked to do. It actually is taken from "Data abstraction & problem sloving with C++" written by Frank M. Carrano. Here it is:

//stock list
class StockList
{
    public:
        //...

    private:
        //node for wait list of people waiting for certain titles
        struct WaitNode
        {
            string ID;
            string who;
            WaitNode *next;
        }; //end WaitNode

        //stock item
        class StockItem
        {
            public:
            //...

            private:
                string ID;
                string title;
                int year, have, want;
        }; // end StockItem

        //node for inventory list of stock items
        struct StockNode
        {
            StockItem item;
            WaitNode *waitHead, *waitTail;
            StockNode *next;
        }; // end StockNode

        // pointer to first stock node on the list
        StockNode *head;
} // end StockLis

By the way, those .... means that we are allowed to fill those parts out. Now the question:

How can I access ID and Who in the waitNode???

>How can I access ID and Who in the waitNode???

// for example
head->waitHead->ID
head->waitTail->Who

They're all private members, so it all has to be accessed within a class function or a function that's a friend of the class.

Yeah, of course, we have to use class functions, but where and how???

That actually is my problem.

>Yeah, of course, we have to use class functions, but where and how???
My guess would be you need to add functions where the "..." ellipsis are in the code. This is really something you need to ask your instructor, though.

It's really weird. At first I thought it's me who can't figure it out. But it seems it's a little bit more complicated than I thought. Anyhow, I will try to think of something.

Thanks a lot.

Meanwhile, if anyone else has any idea, please let me know.

I think that your question is prettymuch unclear, A class nested into another class is necessary for ENCAPSULATION, In this example, The class StockNode can only be used in the member functions and the declarations of StockList.

Then there is the declaration of the variable

StockNode *head; //MEMBER.

And It is fact that THE CLASS StockNode consists of a WaitNode. Which has to be accessed only through the StockNode Pointer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.