|
-
March 27th, 2011, 08:44 PM
#1
Constructors
Hi, ive got what is hopefully a quick question on constructors.
I understand what constructors are there for and why we need them, from looking at this:
GameEngine::GameEngine(HINSTANCE hInstance, LPTSTR szWindowClass,
LPTSTR szTitle, WORD wIcon, WORD wSmallIcon, int iWidth, int iHeight)
{
// Set the member variables for the game engine
m_pGameEngine = this;
m_hInstance = hInstance;
m_hWindow = NULL;
if (lstrlen(szWindowClass) > 0)
lstrcpy(m_szWindowClass, szWindowClass);
if (lstrlen(szTitle) > 0)
lstrcpy(m_szTitle, szTitle);
m_wIcon = wIcon;
m_wSmallIcon = wSmallIcon;
m_iWidth = iWidth;
m_iHeight = iHeight;
m_iFrameDelay = 40; // 25 FPS default
m_bSleep = TRUE;
}
Just so i can confirm something, can you tell me what exactly is supposed to go in a constructors parameters, and what exactly is supposed to go in a constructors body.
Thanks
-
March 27th, 2011, 11:52 PM
#2
Re: Constructors
Constructors construct instances of objects, so typically your parameters are things that identify a particular instance of the object. For example, if the object has a member variable that always needs to be set to zero at creation time, you wouldn't include that in the parameter list because it's always going to be the same for every instance of the object. OTOH if the object has a specific name that is different for each instance, you'd pass that name as a parameter to the constructor. Another typical example is if different instances might have different parent objects (which the object needs to know about) you might pass a parameter so that each instance can identify its own parent.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 28th, 2011, 04:37 AM
#3
Re: Constructors
Oh, can you tell me if ive got it please:
In the constructors body we initialise member variables which are to be the same for every object.
But if we want objects to have a variable which is specific to each individual object we would initialise that variable in the parameters so that when we create an instance of the class, an object, we can pass different parameters to that object, just like when pass different values to a function() when we call it.
Thanks
-
March 28th, 2011, 07:29 AM
#4
Re: Constructors
 Originally Posted by TpOreilly
Oh, can you tell me if ive got it please:
In the constructors body we initialise member variables which are to be the same for every object.
But if we want objects to have a variable which is specific to each individual object we would initialise that variable in the parameters so that when we create an instance of the class, an object, we can pass different parameters to that object, just like when pass different values to a function() when we call it.
Thanks
Not necessarily. How you do it depends on the circumstances of course. It's pretty common to have multiple constructors. A default constructor takes no arguments, and typically will initialize members to a known state. Other constructors can take arguments so non-default values can be passed in when the object is created. This is a convenience more than a requirement.
If you want objects to share a member variable, use the keyword static. That's a completely different topic than constructors and member initialization.
What is the purpose of this line?
m_pGameEngine = this;
-
March 28th, 2011, 07:46 AM
#5
Re: Constructors
It's not quite like that but more or less. Although parameter lists can contain initialisers, you don't initialise member variables there. You pass in values to be used by the constructor for initialisation. Sometimes those values simply get passed to the constructor of a base class. The point is that they're generally values associated with instances rather than common values that would be the same for all objects.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
March 28th, 2011, 08:08 AM
#6
Re: Constructors
ah ok, so, we initialise member variables in the body of constructors. And dont initialise member variables in the parameters, even though we can, we just have parameters in constructors so that when we create an object, that object can use those parameters, am i right this time haha ?
-
March 28th, 2011, 08:18 AM
#7
Re: Constructors
 Originally Posted by TpOreilly
ah ok, so, we initialise member variables in the body of constructors. And dont initialise member variables in the parameters, even though we can, we just have parameters in constructors so that when we create an object, that object can use those parameters, am i right this time haha  ?
I think you're overthinking it.
Classes have members.
A constructor is a special member that is called automatically when an object is created.
You can have as many different constructors with as many different argument types as you like.
What you do in those constructors and how you use the optional arguments is entirely up to you.
Initializing members to non-default values is a typical use of constructor arguments and the default constructor - the one with no arguments - will typically initialize members to some kind of appropriate default value.
-
March 28th, 2011, 09:01 AM
#8
Re: Constructors
I usually do overthink thing, but could you give me a yes or no if im correct in saying that we initialise member variables in the body of a constructor and by giving a constructor parameters we are giving objects those parameters?
-
March 28th, 2011, 09:20 AM
#9
Re: Constructors
Hi,
You certainly can initialise member variables in the body of a constructor, although you could also do it using a constructor intializer list. If you are studying how constructors work, you should definitely take a look intializer lists.
Alan
-
March 28th, 2011, 09:42 AM
#10
Re: Constructors
Thanks for that suggestion. For the time being though im not going to look at initialisation lists as at the moment i only want to understand how to use constructors, which i do now.
-
March 30th, 2011, 03:43 AM
#11
Re: Constructors
For the time being though im not going to look at initialisation lists as at the moment i only want to understand how to use constructors, which i do now.
Well, you have no such an option, as initialization lists are indeed a part of "how to use constructors" story.
Last edited by Igor Vartanov; March 30th, 2011 at 04:20 AM.
Best regards,
Igor
-
March 30th, 2011, 04:11 AM
#12
Re: Constructors
 Originally Posted by TpOreilly
Thanks for that suggestion. For the time being though im not going to look at initialisation lists as at the moment i only want to understand how to use constructors, which i do now.

Dear !
Since as Alexander Suvorov said: "theory without practice is dead", let's stop the discussions until you'll try all your problems/questions in practice (on your computer)!
Victor Nijegorodov
-
March 30th, 2011, 04:12 AM
#13
Re: Constructors
 Originally Posted by TpOreilly
Thanks for that suggestion. For the time being though im not going to look at initialisation lists as at the moment i only want to understand how to use constructors, which i do now.
Do you really think so? Then please try to construct the ConstructMeIfYouCan object below:
Code:
struct foo
{
foo(int x);
};
class ConstructMeIfYouCan
{
foo theFoo;
public:
ConstructMeIfYouCan() {}
};
int main()
{
ConstructMeIfYouCan c;
}
Fix the error and construct c. You must know how to use initialization lists to construct the object.
Regards,
Paul McKenzie
-
March 30th, 2011, 02:26 PM
#14
Re: Constructors
ok, im going to write to my own small programs to show that i understand constructors:
class example
{
public:
int boo;
};
example character;
int main()
{
cout <<character.boo;
}
Here, the output would be 0 because the compiler would have provided the default constructor and set all member variables to 0.
If we put:
class example
{
public:
int boo = 1;
};
example character;
int main()
{
cout <<character.boo;
}
This would result in an error because we are trying to give boo the value of 1 whilst using the default constructor.
If we put:
class example
{
public:
int boo;
example()
{
boo = 1;
}
};
example character;
int main()
{
cout <<character.boo;
}
This would output 1 as we have used our own constructor and initialised boo with the value of 1.
If i put:
class example
{
public:
int boo;
example() : boo(56)
{
}
};
example character;
int main()
{
cout <<character.boo;
}
The output would be 56.
(I have compiled these so i know they work).
-
March 30th, 2011, 03:01 PM
#15
Re: Constructors
 Originally Posted by TpOreilly
ok, im going to write to my own small programs to show that i understand constructors:
First use code tags when posting code.
Second, your example does not require usage of initialization lists. My example requires you to use it, as it will not even compile. Big difference.
There are many rules involving constructors which I can come up with, including examples, that you will more than likely not know. Order of initialization of members (what is the order that members are initialized), questions on multiple inheritance, etc. signature of copy constructors, explict constructor, etc.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 30th, 2011 at 03:03 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|