Little question about constructors

  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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.
  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

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
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

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. }
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I knew somebody wouldn't like that statement. Been too long since I played in cpp, so I was already braced for it.
  • Bozebo
  • Expert
  • Expert
  • User avatar
  • Posts: 709
  • Loc: 404

Post 3+ Months Ago

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
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

That a new thing i learned too. Thanks "Spork"

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 140 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.