www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Properties using

reply Uwar <uwar o2.pl> writes:
Error: Access Violation
When I tring run program below. What is wrong?

//  class CPart
//______________________________________________________
class CPart{
	
	//	utilities
	//__________________________________________________
    class CRing{
		//	data memebers
		//______________________________________________
		private{
			float m_sDia;
			float m_bDia;
		}
		
		//  properties
		//______________________________________________
        public{
            //	property SmallerDiameter
            //__________________________________________
            float SmallerDiameter(float o_sDia){ return m_sDia = o_sDia; }
            float SmallerDiameter(			  ){ return m_sDia		   ; }			
            
            //	property BiggerDiameter
            //__________________________________________
            float BiggerDiameter(float o_bDia){ return m_bDia = o_bDia; }
            float BiggerDiameter(			 ){ return m_bDia		  ; }
        }
	}
	
	//	data members
	//__________________________________________________
	private{
		float m_length;
		CRing m_ringFirst;
		CRing m_ringSecond;
	}

    //  properties
    //__________________________________________________
    public{
        //  property Length
        //______________________________________________
        float Length(float o_length){ return m_length = o_length; }
        float Length(              ){ return m_length           ; }
        
        //  property RingFirst
        //______________________________________________
        CRing RingFirst(){ return m_ringFirst; }
            
        //  property RingSecond
        //______________________________________________
        CRing RingSecond(){ return m_ringSecond; }
    }
}

void 
main(char[][] args){
    CPart part_1;
    with (part_1 = new CPart){
        auto A = RingFirst.SmallerDiameter;
    }
}
Mar 31 2008
next sibling parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 31 Mar 2008 22:51:53 +0200, Uwar <uwar o2.pl> wrote:

 Error: Access Violation
 When I tring run program below. What is wrong?

 [Snip]
m_ringFirst is never initialized. Create a constructor and add m_ringFir= st = =3D new CRing();. Should fix it. -- Simen
Mar 31 2008
parent Uwar <uwar o2.pl> writes:
Great! It is working. Thanks a lot. 
Mar 31 2008
prev sibling parent "Craig Black" <craigblack2 cox.net> writes:
Looks like you are coming from C++ and expect classes to serve as value 

structs are value types.  You may want to use structs instead of classes.

-Craig
Mar 31 2008