Skip to main content
GameDev.net gamedev.net
🔒 Locked

initializing private instance variables

Started by Pfhorseti May 30, 2002 at 2:37 AM 8 replies 250+ views
Original Post
Pfhorseti
Pfhorseti
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?
-forseti
Chem0sh
Chem0sh
You mean in the construtor? Where are you initializing?
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Pfhorseti
Pfhorseti
Yes, in the constructor.
-forseti
Chem0sh
Chem0sh
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 */
/*=========================================*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/
Pfhorseti
Pfhorseti
thanks
-forseti
DerekSaw
DerekSaw
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

{
}
"after many years of singularity, i'm still searching n the event horizon" o
Vasialek
Vasialek
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
siaspete
siaspete
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
DerekSaw
DerekSaw
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]
"after many years of singularity, i'm still searching n the event horizon" o
Qw3r7yU10p!
Qw3r7yU10p!
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.