Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Default constructors vs default arguments


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Sir Ementaler   Members   -  Reputation: 449

Like
0Likes
Like

Posted 22 August 2013 - 05:45 AM

Let's consider a simple class declared within a script:

class foo{
  foo(){
    //...
  }
}

I can create its instances in both of these two ways:

foo foo1();
foo foo2;

Now let's consider a similar class, but give its constructor a parameter that can have a default value:

class foo{
  foo(int bar=0){
    //...
  }
}

This will still be valid:

foo foo1();

But the other line will return an error:

ERR : No default constructor for object of type 'foo'.

 

This error will also appear in various other cases such as trying to create a temporary copy of an object. I think that constructors which can be called with no parameters should be treated as default constructors.


Edited by Sir Ementaler, 22 August 2013 - 05:51 AM.


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3227

Like
0Likes
Like

Posted 22 August 2013 - 04:35 PM

I'll look into it.

 

Declaring a variable of the type with the default arg should definitely work and is something that I've missed in the implementation of default args.

 

However, to allow the compiler to create temporary objects using a non-default constructor with default args doesn't sound like a good idea.


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

#3 Sir Ementaler   Members   -  Reputation: 449

Like
0Likes
Like

Posted 23 August 2013 - 07:14 AM

I'm almost entirely convinced that if somebody creates a constructor that can be called without any arguments, their intention is making it a default constructor. I was trying to imagine a case in which this could generate problems but I'm unable to. I'm pretty sure a constructor whose all arguments have default values should overtake all functions of a default constructor including creating temporary objects, even if only because creating a proper default constructor would become impossible due to a "multiple matching signatures" error. I'm not as experienced programmer and I won't be surprised if I had overlooked some downside of this solution so if you could explain why exactly it sounds like a bad idea, I would be thankful.



#4 Andreas Jonsson   Moderators   -  Reputation: 3227

Like
1Likes
Like

Posted 01 December 2013 - 03:26 PM

I've implemented this in revision 1792.

 

Thanks,

Andreas


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS