473,839 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Assign class memer in body of constructor

Hello!

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

We can also initialize in this way
AccountForStude nt::AccountForS tudent(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

AccountForStude nt::AccountForS tudent(Student s, double balance) :
{
stud_ = s;
balance_ = balance;
}
Jul 23 '05 #1
3 1405
* Tony Johansson:

We can also initialize in this way
AccountForStude nt::AccountForS tudent(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 "constructi on 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.ht ml>

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 AccountForStude nt defined in this
way
AccountForStude nt::AccountForS tudent(Student s, double balance) : stud_(s),
balance_(balanc e)
{} //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
AccountForStude nt::AccountForS tudent(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

AccountForStude nt::AccountForS tudent(Student s, double balance) :
{
stud_ = s;
balance_ = balance;
}


V
Jul 23 '05 #3
Tony Johansson wrote:

Hello!

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

We can also initialize in this way
AccountForStude nt::AccountForS tudent(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.

AccountForStude nt::AccountForS tudent(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
6398
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
4
12589
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)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
6
3705
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 line below: pNotify = new EMAILnotify; that lives in the Notification Constructor. One theory is that the Notify base class is not completely constructed prior to using it in the Notification Constructor code: pNotify = new EMAILnotify;
13
2397
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. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
11
3102
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 class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function Now to my problem.
16
25430
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
5
2318
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 temp=0):a(temp){} A(A& right):a(right.a){} };
4
4173
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? class Parent { Parent::Parent() { child = new Child( this) }; Child *child; };
10
4710
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 no-parameter constructor for my class with an empty function body. I then instantiated an object and tried printing its values, amazingly the members were already initialized. But how is this possible if I have not included any code for doing so. The...
0
10908
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10586
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10648
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9426
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7828
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7017
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5866
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4484
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3134
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.