Constructor limitations?

BlitzMax Forums/BlitzMax Programming/Constructor limitations?

verfum(Posted 2007) [#1]
Hi, my native is c++ but I'm playing with max, I'm having problems getting the constructors to work the way I want, is there a way of doing something similar to you would in c++:

Type Position
	Field m_x:double, m_y:double;
	
	Function Create:Position(x:double, y:double)
		Local pos:Position = New Position;
		pos.m_x = x;
		pos.m_y = y;
		return pos;
	
	End Function
	
	Method getX:Double()
		return m_x;	
	End Method
	
	Method getY:Double()
		return m_y;
	End Method
	
	Method setX:Double(x:Double)
		m_x = x;
	End Method
	
	Method setY:Double(y:Double)
		m_y = y;
	End Method

End Type

Type Entity
	Field m_position:Position;
	
	Function Create:Entity( position:Position )
		local ent:Entity = new Entity;
		ent.m_position = position;
		return ent;	
		
	End Function
	
	Method getPosition:Position()
		return m_position;
	End Method
	
	Method setPosition:Position(position:Position)
		m_position = position;
	End Method

End Type

Type Player Extends Entity
	
	Function Create:Player(position:Position)
        local ent:Player = New Player;
		' Not sure what to put now for inheriting from Entity?
		
		Return ent;
	
	End Function
	
End Type

'And this is what I want to do, which you would do in c.
myPlayer:Player = Player.Create(Position(10, 10));


And the constructor in c++
Enemy(const Position& position)
:Entity(position)
{
}

That's it, this is how c++ inherits from the base entity, I cant seem to do this is Bmax?

Thanks.


ziggy(Posted 2007) [#2]
Your code is right, Be sure to do things like:
Local MyEntity:Entity = Entity.Create(Position.Create(100,100))



verfum(Posted 2007) [#3]
Oh yeah, forgot about that, that's so annoying!

But the code must be wrong, because I can't have the code that way?

Here is a construction from a game I worked on in c++
Enemy enemy = level.addEnemy(new Enemy(Position(200,200), 45, Image(".\\gfx\\tank.bmp")));



verfum(Posted 2007) [#4]
OH! i've just noticed what you have done!! standye ....


verfum(Posted 2007) [#5]
Oh cool, that was the problem, thanks, now to test to see if Bmax constructors are as hard as c++ <rubs hands>


ziggy(Posted 2007) [#6]
Yes, the sintax is different. 'core' constructor is NEW an, as blitzMax does not have overloading, you have to create other constructors, so you have to speficy the constructor method name in the call.
Local MyEnemy = level.AddEnemy(enemy.create(Position.Create(200,200),40,Image(".\\gfx\tank.bml")))

It is just another sintax to do the same.


verfum(Posted 2007) [#7]
Oh god your right!! I can't overload the constructors!! Which means I can't do the object orientation the way I want :( hmm!


Czar Flavius(Posted 2007) [#8]
	Method setX:Double(x:Double)
		m_x = x;


These functions do not return a value, yet are set as Double. Also are they really needed?


Beaker(Posted 2007) [#9]
Are you using SuperStrict?


Ked(Posted 2007) [#10]
I'm not sure you need the comma after "m_x = x;". But since no one has brought it up, I'm probably wrong.


Azathoth(Posted 2007) [#11]
He can use them if he really wants, but they're not required unless multiple lines are being fit on one line.


N(Posted 2007) [#12]
verfum, you can't have constructors with arguments, so although you can call the base class's constructor, it wouldn't do any good(and would probably result in a compiler error, since I don't know that you can directly call either the constructor or destructor).

Your best bet is to write a function to create a new object, then call a method that will initialize the object with the arguments passed to the function. Something like this:
Type Foo Extends Bar
    Method _initfoo( i:Int )
        _initbar( something else )
        do some more stuff here, look important
    End Method
End Type

Function CreateFoo:Foo( i:Int )
    Local this:Foo = New Foo
    this._initfoo( i )
    Return this
End Function
I wouldn't recommend placing a Create function in the type, as it might prove troublesome if it turns out you need to have the same arguments as a base class's Create function. Can't remember if that is the case, so you'd have to test it to find out.

Ked: It's a semi-colon, not a comma.


plash(Posted 2007) [#13]
I'm not sure you need the comma after "m_x = x;". But since no one has brought it up, I'm probably wrong.


It could of been from c++ code, and he just didn't remove it.


Czar Flavius(Posted 2007) [#14]
I like ; anyway because they look nice.