Little question about constructors

  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 15, 2006
  • Posts: 709
  • Loc: 404
  • Status: Offline

Post January 18th, 2010, 4:12 am

Though I am using c++ for this question, the principle applies to probably all OOP languages.

When using a constructor, or a setter method. I end up with something like this:
Code: [ Select ]
class gameObject{
 public:
 long x,y; //location

 gameObject(long setX,long setY){
  x = setX;
  y = setY;
 }
}
  1. class gameObject{
  2.  public:
  3.  long x,y; //location
  4.  gameObject(long setX,long setY){
  5.   x = setX;
  6.   y = setY;
  7.  }
  8. }

What I am doing is just taking the x and y passed by the constructor's arguments - is there a clever way of doing this? I don't like my way and I would suspect there is a better way, just not sure how to approach somebody and ask about it :D
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post January 18th, 2010, 4:12 am

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post January 18th, 2010, 6:33 am

That's the proper way to initialize variables in C++. Some languages have different ways to set default or shorthand ways of accomplishing the same thing, but this is the standard readable solution IMO.
I'd love to change the world, but they won't give me the source code.
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post January 18th, 2010, 7:12 am

I've become a fan of constructors that set default properties and require instance-specific changes to be made through the setters or property operators after instantiation.

I just find that a lot of times my objects use a lot of the same arguments and passing the same default arguments to multiple instantiations becomes a hassle.
Strong with this one, the sudo is.
  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 15, 2006
  • Posts: 709
  • Loc: 404
  • Status: Offline

Post January 18th, 2010, 8:18 am

joebert wrote:
I've become a fan of constructors that set default properties and require instance-specific changes to be made through the setters or property operators after instantiation.

I just find that a lot of times my objects use a lot of the same arguments and passing the same default arguments to multiple instantiations becomes a hassle.


yeah ive got a bit on the expanded code that is something like:
Cenemy(long x,long y,int health = 10):CgameObject(x,y);
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Joined: Sep 22, 2003
  • Posts: 6134
  • Loc: Seattle, WA
  • Status: Offline

Post January 18th, 2010, 11:19 am

UPSGuy wrote:
That's the proper way to initialize variables in C++. Some languages have different ways to set default or shorthand ways of accomplishing the same thing, but this is the standard readable solution IMO.

Actually, not quite.

When you do it that way, you're taking an unnecessary step with each variable by first creating the variable, initializing it to a default (usually garbage), then reassigning a new value to them in the constructor body.

C++ has a construct that allows you to initialize properties in the constructor with a value at the time of creation:

CPP Code: [ Select ]
class GameObject {
 
private:
  long x, y;
 
public:
  GameObject();
 
};
  1. class GameObject {
  2.  
  3. private:
  4.   long x, y;
  5.  
  6. public:
  7.   GameObject();
  8.  
  9. };


CPP Code: [ Select ]
GameObject::GameObject(long initX, long initY)
  : x(initX), y(initY) {
 
  // nothing needed here
}
  1. GameObject::GameObject(long initX, long initY)
  2.   : x(initX), y(initY) {
  3.  
  4.   // nothing needed here
  5. }
The Beer Monocle. Classy.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post January 18th, 2010, 11:35 am

I knew somebody wouldn't like that statement. Been too long since I played in cpp, so I was already braced for it.
I'd love to change the world, but they won't give me the source code.
  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Joined: Feb 15, 2006
  • Posts: 709
  • Loc: 404
  • Status: Offline

Post January 18th, 2010, 5:08 pm

Thanks for the knowledge spork. I wouldn't have known the answer to that for ages if you hadn't posted. :idea:
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 480
  • Loc: India
  • Status: Offline

Post January 19th, 2010, 4:43 am

That a new thing i learned too. Thanks "Spork"
Amit
My Blog http://www.amityadav.name

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 142 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.