Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assign class memer in body of constructor

Reply
Thread Tools

Assign class memer in body of constructor

 
 
Tony Johansson
Guest
Posts: n/a
 
      05-20-2005
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;
}


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-20-2005
* 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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-20-2005
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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      05-20-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Static data memer Surya Kiran C++ 5 02-05-2004 02:57 PM



Advertisments