Sign in to follow this  
kunitoki

how to call inner classes constructors (with arguments) ?

Recommended Posts

i'm trying to get the best out of the new features of 2.6.0, especially when relating with the classes. i have to declare a class member which is actually another class (c++ or script class doesn't matter). this member have only a class constructor that takes arguments, and i don't know how to call the constructor: <angelscript> class ScriptComponent { ScriptComponent(const String& obj) { // how to call constructor for this.self ? } RealComponent self; }; </angelscript> now... RealComponent constructor takes two arguments... how to correctly initialize it, since it cannot have assignment operator registered (and so "this.self = createRealComponent();" cannot be called) ? i've tried doing a "this.self(arguments);" in MyComponent constructor but no way... any thoughts about this ?

Share this post


Link to post
Share on other sites
In C++, the only way to have members who do not have default constructors is to use the initializer list. In C++;


class Foo
{
Bar m_bar;
public:
Foo() : m_bar(parameters) {}
};


I don't think Angelscript supports this, and thus theres no way to do it yet.

Share this post


Link to post
Share on other sites
yeah man, thanx you... c++ is fine here ;)
i think that a lot of features for class support have to be done in angelscript before thinking about inheritance and such...

anyway... happy easter !

Share this post


Link to post
Share on other sites
Indeed, this hasn't been implemented yet.

Would it be possible for you to use a two-step initialization for the class? I.e:


class ScriptComponent
{
ScriptComponent(const String& obj)
{
// self is initialized with default constructor,
// now call the init function to initialize the rest.
self.init();
}

RealComponent self;
};

Share this post


Link to post
Share on other sites
yeah for sure is possible. i've already done that way. but i asked for clarify myself of this lackness. i prefer have a clear way of doing things and i think when relating with classes, every part of the problem should be addressed.
i think that not having different than default constructor in classes isn't a gr8 miss, but indeed, is somelike a base part of a language (script or not), a perfect logical block when dealing with classes: why i have to duplicate the constructors with functions that do the same things ? in some cases i can use constructors and in other i should use initFunctions, that practically mimics the first...

Share this post


Link to post
Share on other sites
I agree with you, and AngelScript will definitely support the use of non-default constructors for class members as well. I just haven't gotten that far yet. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this