www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



digitalmars.D.learn - getting a classes array

↑ ↓ ← 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
↑ ↓ → "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 initialize a member like in Java/C#. 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