CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Constructors

  1. #1
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Question 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

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    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

  3. #3
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Constructors

    Quote Originally Posted by TpOreilly View Post
    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;

  5. #5
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    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

  6. #6
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    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 ?

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Constructors

    Quote Originally Posted by TpOreilly View Post
    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.

  8. #8
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    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?

  9. #9
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  10. #10
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    Talking 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.


  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Constructors

    Quote Originally Posted by TpOreilly View Post
    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

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructors

    Quote Originally Posted by TpOreilly View Post
    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

  14. #14
    Join Date
    Feb 2011
    Location
    UK
    Posts
    73

    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).

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructors

    Quote Originally Posted by TpOreilly View Post
    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.

Page 1 of 2 12 LastLast

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
  •  





Click Here to Expand Forum to Full Width

Featured