By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,257 Members | 928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,257 IT Pros & Developers. It's quick & easy.

Assign class memer in body of constructor

P: n/a
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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* 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?
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
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
kb******@gascad.at
Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.