www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Subclass->Base->Subclass help

reply Chris Williams <littleratblue yahoo.co.jp> writes:
Well I have some code that causes an access violation under DMD v2.012. I've
distilled it down to a mini-program that demonstrates the issue, but I don't
know if it's me doing something wrong or what.

Here's the code:

import std.stdio;

class SomeItem {
public:
	bool some_var;
}

class BaseClass {
protected:
	SomeItem alpha;
	
public:
	this(SomeItem si) {
		alpha = si;
		si.some_var = true;
	}
}

class NewItem : SomeItem {
public:
	bool new_var;	
}

class ExtClass : BaseClass {
protected:
	NewItem alpha;
	
public:
	this() {
		NewItem temp = new NewItem();
		temp.new_var = false;
		super(temp);
	}
	
	void test_vals() {
		if (alpha.some_var) {
			writefln("some_var is true");
		}
		
		if (alpha.new_var) {
			writefln("new_var is true");
		}
	}
}

int main(char args[][]) {
	ExtClass test = new ExtClass();
	test.test_vals();
	
	return 0;
}
Apr 15 2008
parent boyd <gaboonviper gmx.net> writes:
The problem here is that alpha in ExtClass doesn't override the alpha in=
  =

the BaseClass. it just hides it. so Baseclass.alpha is not the same as  =

ExtClass.alpha.

You can handle this by casting

   class BaseClass {
   protected:
  	SomeItem _alpha;
   private:
         SomeItem alpha(){ return _alpha; }
   public:
  	this(SomeItem si) {
  		_alpha =3D si;
  		si.some_var =3D true;
  	}
   }

  class ExtClass : BaseClass {
  private:
  	NewItem alpha(){ return cast(NewItem)_alpha; }
  	=

  public:
  	this() {
  		NewItem temp =3D new NewItem();
  		temp.new_var =3D false;
  		super(temp);
  	}
  	=

  	void test_vals() {
  		if (alpha.some_var) {
  			writefln("some_var is true");
  		}
  		=

  		if (alpha.new_var) {
  			writefln("new_var is true");
  		}
  	}
  }

It's not the prettiest way to do it, but it works. Still I'd suggest  =

trying a different approach. Perhaps rethink the design.

Cheers,
Boyd

---------
On Tue, 15 Apr 2008 09:20:32 +0200, Chris Williams  =

<littleratblue yahoo.co.jp> wrote:

 Well I have some code that causes an access violation under DMD v2.012=

 I've distilled it down to a mini-program that demonstrates the issue, =

 but I don't know if it's me doing something wrong or what.

 Here's the code:

 import std.stdio;

 class SomeItem {
 public:
 	bool some_var;
 }

 class BaseClass {
 protected:
 	SomeItem alpha;
 	=

 public:
 	this(SomeItem si) {
 		alpha =3D si;
 		si.some_var =3D true;
 	}
 }

 class NewItem : SomeItem {
 public:
 	bool new_var;	=

 }

 class ExtClass : BaseClass {
 protected:
 	NewItem alpha;
 	=

 public:
 	this() {
 		NewItem temp =3D new NewItem();
 		temp.new_var =3D false;
 		super(temp);
 	}
 	=

 	void test_vals() {
 		if (alpha.some_var) {
 			writefln("some_var is true");
 		}
 		=

 		if (alpha.new_var) {
 			writefln("new_var is true");
 		}
 	}
 }

 int main(char args[][]) {
 	ExtClass test =3D new ExtClass();
 	test.test_vals();
 	=

 	return 0;
 }

-- = Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Apr 15 2008