Assign class memer in body of constructor

T

Tony Johansson

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;
}
 
A

Alf P. Steinbach

* 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.
 
V

Victor Bazarov

Tony said:
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
 
K

Karl Heinz Buchegger

Tony said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top