473,323 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Assign class memer in body of constructor

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
3 1376
* 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
6
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
5
by: flamexx7 | last post by:
In the programme below is it possible to call copy constructor of class A, inside copy constructor of class B. #include <iostream> using namespace std; class A{ int a; public: A(int...
4
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called?...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.