Assign class memer in body of constructor

Discussion in 'C++' started by Tony Johansson, May 20, 2005.

  1. Hello!

    Assume you have a constructor for class AccountForStudent defined in this
    way
    AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s),
    balance_(balance)
    {} //Here in stud_(s) above we call the copy constructor

    We can also initialize in this way
    AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s)
    { //Here in stud_(s) above we call the copy constructor
    balance_ = balance;
    }

    And we have a third alternative and that is to use assignment operator to
    assign to the stud_ object.
    Now to my qustion here in statement stud_ = s we call the assignment
    operator and
    there is one requirement to be able to assign like we have done and that is
    that there must exist a no-arg constructor for class Student.
    My question why? I can't see any connection between a no-arg constructor and
    an assignment operator.
    When you initialize using the initialization list you don't have any
    requirement that a no-arg constructor must exist.

    //Tony

    AccountForStudent::AccountForStudent(Student s, double balance) :
    {
    stud_ = s;
    balance_ = balance;
    }
     
    Tony Johansson, May 20, 2005
    #1
    1. Advertising

  2. * Tony Johansson:
    >
    > We can also initialize in this way
    > AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s)
    > { //Here in stud_(s) above we call the copy constructor
    > balance_ = balance;
    > }
    >
    >...
    > there is one requirement to be able to assign like we have done and that is
    > that there must exist a no-arg constructor for class Student.
    > My question why?


    Presumably you mean if you assign to 'stud_' instead of using the
    constructor initalization list.

    In the constructor body you have access to 'stud_', and the rules of C++ are
    designed to give you a guarantee that anything of class type you have access
    to is initialized, the "construction guarantee".

    If there's no default constructor then (in this case) the guarantee can't be
    honored, and the compiler must spit out a diagnostic message.

    You can read more about the basics here:

    <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_02.html>

    Also see the FAQ.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, May 20, 2005
    #2
    1. Advertising

  3. Tony Johansson wrote:
    > Assume you have a constructor for class AccountForStudent defined in this
    > way
    > AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s),
    > balance_(balance)
    > {} //Here in stud_(s) above we call the copy constructor


    And you actually cause another copy constructor to be called to create the
    argument 's'. Just so that you know...

    > We can also initialize in this way
    > AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s)
    > { //Here in stud_(s) above we call the copy constructor
    > balance_ = balance;
    > }


    Which makes only difference for 'balance_' member.

    > And we have a third alternative and that is to use assignment operator to
    > assign to the stud_ object.
    > Now to my qustion here in statement stud_ = s we call the assignment
    > operator and
    > there is one requirement to be able to assign like we have done and that is
    > that there must exist a no-arg constructor for class Student.
    > My question why?


    Because before you can assign anything to 'stud_', it has to be somehow
    constructed. And since you omitted it from the initialiser list, the
    compiler has no other choice but to construct it using the _default_
    constructor. That's the rule.

    > I can't see any connection between a no-arg constructor and
    > an assignment operator.


    There is no connection.

    > When you initialize using the initialization list you don't have any
    > requirement that a no-arg constructor must exist.


    Correct.

    >
    > //Tony
    >
    > AccountForStudent::AccountForStudent(Student s, double balance) :
    > {
    > stud_ = s;
    > balance_ = balance;
    > }


    V
     
    Victor Bazarov, May 20, 2005
    #3
  4. Tony Johansson wrote:
    >
    > Hello!
    >
    > Assume you have a constructor for class AccountForStudent defined in this
    > way
    > AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s),
    > balance_(balance)
    > {} //Here in stud_(s) above we call the copy constructor
    >
    > We can also initialize in this way
    > AccountForStudent::AccountForStudent(Student s, double balance) : stud_(s)
    > { //Here in stud_(s) above we call the copy constructor
    > balance_ = balance;
    > }
    >
    > And we have a third alternative and that is to use assignment operator to
    > assign to the stud_ object.
    > Now to my qustion here in statement stud_ = s we call the assignment
    > operator and
    > there is one requirement to be able to assign like we have done and that is
    > that there must exist a no-arg constructor for class Student.


    Yes and no.
    Such a requirement exists. But for a different reason.

    > My question why? I can't see any connection between a no-arg constructor and
    > an assignment operator.


    There is no connection.

    >
    > AccountForStudent::AccountForStudent(Student s, double balance) :
    > {


    Here. At this point in time, the member stud_ must already exist and
    be properly initialized. Since you didn't specify how this initialization
    should be done (you have nothing in the initializer list that would specify
    this), the compiler falls back to the default rule, which is: use the default
    constructor (the one with no required arguments) to initialize that member.

    So this is the real reason why you have to have a default constructor in this
    case: Because the compiler needs it to initialize the member to a default state ...

    > stud_ = s;


    .... which gets later changed by executing this assignment.

    > balance_ = balance;
    > }



    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, May 20, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Surya Kiran

    Static data memer

    Surya Kiran, Jan 28, 2004, in forum: C++
    Replies:
    5
    Views:
    334
    Victor Bazarov
    Feb 5, 2004
  2. mrstephengross
    Replies:
    5
    Views:
    617
    Larry I Smith
    May 18, 2005
  3. ali
    Replies:
    4
    Views:
    605
    David Harmon
    Mar 5, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,236
    terminator
    Nov 7, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,320
Loading...

Share This Page