• Advertisement

Archived

This topic is now archived and is closed to further replies.

initializing private instance variables

This topic is 5808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In OOP, when you initialize private instance variables, does it matter if you call it through self, like self.setRectX(0) or directly aRect.x = 0?

Share this post


Link to post
Share on other sites
Advertisement
It doesn''t really matter, but the second way will avoid the overhead of calling the SetX method.


  
class ARect
{
public:
ARect(void);
// etc, etc...


private:
int x;
// etc.

};

ARect::ARect(void)
{
x = 0;
}



There ya go!


/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

Share this post


Link to post
Share on other sites
It is better to initialize a variable in the Initialization List.
eg.


  
class ARect
{
public:
ARect();

private:
int x;
};
ARect::ARect()
: x(0) // this is the initialization list

{
}

Share this post


Link to post
Share on other sites
DerekSaw, could u explain WHY it is better to initialize variables through list? I''ve heard about this, but not answer to question WHY.

Lekha

Share this post


Link to post
Share on other sites
I''m by no means an expert, but as far as I know it''s because the objects you''re initialising may have already been initialised by their own constructors.

By doing this in the initialisation list you''re specifying how to call the objects'' constructors. Basically it can avoid objects being initialised twice.


Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions

Share this post


Link to post
Share on other sites
There's no concrete reason why one SHOULD/MUST use Initialization List over normal assignment... just to show...

// similar to assignment inside constructor
int a;
a = 0;
...
// this is what initialization list looks like
int a = 0;
...


What if it is some user defined class instead of int?

SomeObj a;
a = some_initial_value;
...
// OR
SomeObj a = some_initial_value;
...


The 1st looks like it got two-step creation:
1. default constructor called
2. assignment to some_initial_value

The 2nd one just got:
1. constructor(that accept argument of type some_initial_value) called.

One thing for sure, if you have some constant variable (declared with the const ) in your class, you can't assign them in the constructor. The only place to assign (or a better word, initialize) them, is in the Initialization List. eg.

Ah, references too must be initialized in the IL.

// u can't do this
const int a;
a = 0;
SomeObj & b;
b = someObj2;
...
// but u can...
const int a = 0;
SomeObj & b = someObj2;
...


... anyone have some better explanation on these?

[edited by - DerekSaw on May 30, 2002 9:39:53 PM]

Share this post


Link to post
Share on other sites
when an object of your class type is created the members are also created. you can choose how they are initialsed (it may even be necessary). you do this in the initialisation list. if you don`t put anything then they are still created but just with the default constructor (as long as it exists... see above comment). you might as well initialise your variables here rather than in the body of the constructor as anything you put there will be an overhead.

Share this post


Link to post
Share on other sites

  • Advertisement