How to make Nested class, which use default constructor parameters?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • osfreak
    New Member
    • Oct 2008
    • 22

    How to make Nested class, which use default constructor parameters?

    Code:
    class Outer
    {
    public:
    	int outera;
    
    	class Inner{
    
    	public:
    		static int a,b;
    		int c;
    		void Displaystatvalue();
    		Inner(int parm = 0):c(parm)
    		{
    		}
    		
    	}in1;	     //}in1(15);//Not allowed why
            Inner in2;   //Inner in2(10);//not allowed,why?
    
    };
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    in1 and in2 are member variables. in(15) and in2(10) are constructor calls. That is, actual code that executes and allocates memory. in(15)and in2(10) are , therefore definitions (objects created at run time), and you cannot have definitions inside a declaration because there's no way to make those calls at compile time.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      If you want to initialise the members of a class then you do it in the class initialisation list.

      The initialisation list is something that can only be attached to a constructor and it specifies how to initialise class members as well as base classes.

      In your case something like

      Code:
      Outer::Outer() :
        in2(10)
      {
      }
      in much the same way you initialise the C member of Inner

      Comment

      Working...