www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getting a classes array

reply lurker <lurker lurker.com> writes:
the following chrashes what do i do wrong? how does one manipulate class A's
mVector array?

thanks


class A {
private:
	int mVector[20] = 0;
public:
	int* Self() { 
	  return  mVector.ptr; 
	}
	bool StatusErrors() {
	  if((this.mVector[0] == 1) && (this.mVector[1] > 0))
		  return true;
	  return false; 
	}
	void Reset() {
		for(int i = 0; i < 20; i++)
		  mVector[i] = 0;
	}
}

class B {
private:
  A aa = new A;
public:
  bool test() {
    ....
    do some stuff
    int i = somefunction(aa.Self());
		if(aa.StatusErrors())
			return false;
		aa.Reset();                          <------ crashes here
    int i = somefunction1(aa.Self());
		if(aa.StatusErrors())
			return false;
	}
}
May 02 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"lurker" <lurker lurker.com> wrote in message 
news:fvgv7p$1gu7$1 digitalmars.com...

 void Reset() {
 for(int i = 0; i < 20; i++)
   mVector[i] = 0;
 }
 }
Slicing is fun. That loop can be replaced with "mVector[] = 0;"
 class B {
 private:
  A aa = new A;
This line doesn't even compile. You can't use a 'new' expression to A aa; this() { aa = new A; }
  bool test() {
    ....
    do some stuff
    int i = somefunction(aa.Self());
 if(aa.StatusErrors())
 return false;
 aa.Reset();                          <------ crashes here
    int i = somefunction1(aa.Self());
 if(aa.StatusErrors())
 return false;
 }
 }
The only error I get when I run this code is an assertion at the end of test() saying that it's missing a return. You'll only get that error if you compile with -debug though. I don't get any error upon calling Reset.
May 03 2008