473,652 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

double re, im; re(r), im(i) vs re=r, im=i

On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor. What I'm not clear on is the implication
of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the
text? Where can it be used? How is it different from re=r? How should I
read in in English?

--STH
Jul 22 '05 #1
10 1667
* Hattuari <su******@setid ava.kushan.aa> schriebt:
On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor.
Right.

What I'm not clear on is the implication
of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the
text?
Don't know since I don't have the text; presumably you could find that
out yourself?

Where can it be used? How is it different from re=r? How should I
read in in English?


FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.

Not mentioned or prominently mentioned in the FAQ: you should (ideally)
never depend on declaration order to provide some required initialization
order, because it's subtle and easy to forget or misunderstand. This might
mean that you have to use assignment instead of initializers. It might also
mean a redesign from using direct member objects to using pointers, or, even
better, to remove the need for a particular initialization order.

Jul 22 '05 #2
"Hattuari" <su******@setid ava.kushan.aa> wrote in message
news:y5******** ************@sp eakeasy.net...
On page 32 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i){re=r; im=i;}
//...
};

On page 262 of TC++PL SE is an example of a complex class as follows:

class complex {
double re, im;
public:
complex(double r, double i):re(r), im(i){}
//...
};

I understand that in the latter the re(r) and im(i) are in the member
initialization list, whereas in the former the re=r; and im=i; are in the
function body of the constructor. What I'm not clear on is the implication of doing things one way or the other. Also confusing is the difference in
syntax between re=r and re(r). Where is this latter form introduced in the text? Where can it be used? How is it different from re=r? How should I
read in in English?

--STH


I don't have your text so I can't answer any of your questions about where
things might be found.

I would say that member initialization should always be preferred to
assignment in the constructor body if both will work. The key point is that
before your constructor runs, all member variables must be constructed
somehow. If they are classes, running a default constructor first and then
overriding with an assignment could turn out to be slow.

With plain old data types like double it may not be clear why you would use
the syntax re(r). But if your member is a class, it may need constructor
arguments:

class Fred
{
Ethyl m_e;
public:
Fred(int blort) : m_e(blort) {}
};

That explains the re(r) syntax for classes, but how can the same thing work
for plain old data? It's not so strange if you know that in C++ the
following is legal:

int k(5);

It has the same effect as

int k = 5;

So when would you not use member initialization? When you have something
more complicated to do than just copying the constructor arguments:

class Mabel
{
int m_x;
double m_y;
public:
Mabel() {complicated_fu nction_to_set_x _and_y(&m_x, &m_y);}
};

In my code this doesn't come up all that often.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt:
On page 32 of TC++PL SE is an example of a complex class as follows:

Don't know since I don't have the text; presumably you could find that
out yourself?
One might also presume that I looked before asking the question. Perhaps I
should simply glean the implication from his passing comment that all
instances of fundamental types are objects in C++.
Where can it be used? How is it different from re=r? How should I
read in in English?


FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.


That doesn't seem to discuss the difference between the two forms presented
in my question.
Not mentioned or prominently mentioned in the FAQ: you should (ideally)
never depend on declaration order to provide some required initialization
order, because it's subtle and easy to forget or misunderstand. This
might
mean that you have to use assignment instead of initializers. It might
also mean a redesign from using direct member objects to using pointers,
or, even better, to remove the need for a particular initialization order.


Yes, I've always found such dependencies to be objectionable. Sometimes
they are necessary, such as when one subsystem relies on a reference to
another for it's initialization data. Sometimes the alternatives are even
worse. At that point I usually document the dependencies and move on.
Jul 22 '05 #4

"Hattuari" <su******@setid ava.kushan.aa> wrote in message
news:fv******** ************@sp eakeasy.net...
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt:
On page 32 of TC++PL SE is an example of a complex class as follows:


Don't know since I don't have the text; presumably you could find that
out yourself?


One might also presume that I looked before asking the question.


See pp 247-248, Section 10.4.6 -- Class Objects as Members
(I have 3rd Edition, the page numbers might not match exactly
in your Special Edition).

-Mike
Jul 22 '05 #5
Cy Edmunds wrote:
With plain old data types like double it may not be clear why you would
use the syntax re(r). But if your member is a class, it may need
constructor arguments:

class Fred
{
Ethyl m_e;
public:
Fred(int blort) : m_e(blort) {}
};

That explains the re(r) syntax for classes, but how can the same thing
work for plain old data? It's not so strange if you know that in C++ the
following is legal:

int k(5);

It has the same effect as

int k = 5;


Thanks, that addresses the essence of my question. At this point I can only
take it for granted that it works. A theoretical explanation would be nice.
I don't know if Stroustrup offers one or not. I have to admit I set the
book down for several months, and don't recall all of the discussion in the
previous chapters. Looking in the index, and thumbing through the chapters
has not led me to an explanation.
Jul 22 '05 #6
* Hattuari <su******@setid ava.kushan.aa> schriebt:
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt:
On page 32 of TC++PL SE is an example of a complex class as follows:

Don't know since I don't have the text; presumably you could find that
out yourself?


One might also presume that I looked before asking the question.


What in the world are you talking about?
Where can it be used? How is it different from re=r? How should I
read in in English?


FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.


That doesn't seem to discuss the difference between the two forms presented
in my question.


It does or should -- it is the section of the FAQ dedicated to that
question.

Comments on the FAQ can be sent to Marshall Cline, the FAQ maintainer,
at <url: mailto:cl***@pa rashift.com>.

At least they could, earlier, but he's got an ever increasing amount of
spam to that address.

Jul 22 '05 #7
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt:
>> Where can it be used? How is it different from re=r? How should I
>> read in in English?
>
> FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.


That doesn't seem to discuss the difference between the two forms
presented in my question.


It does or should -- it is the section of the FAQ dedicated to that
question.


My question was actually subtler than what is addressed in the FAQ. The FAQ
item has to do with the difference between the two approaches to
constructing the Complex class. I guess I wasn't clear enough since a few
people seem to have assumed I was asking the question addressed in the
item. What I really wanted to know is what exactly does double re; re(r);
mean? I believe the answer is that all fundamental type in C++ are
actually objects. I believe he states as much in Chapter 10. I can't find
it right now. I guess Stroustrup didn't think it important enough to
warrent more than a passing comment.
Jul 22 '05 #8
* Hattuari <su******@setid ava.kushan.aa> schriebt:
Alf P. Steinbach wrote:
* Hattuari <su******@setid ava.kushan.aa> schriebt:
>> Where can it be used? How is it different from re=r? How should I
>> read in in English?
>
> FAQ: <url: http://www.parashift.c om/c++-faq-lite/ctors.html#faq-10.6>.

That doesn't seem to discuss the difference between the two forms
presented in my question.


It does or should -- it is the section of the FAQ dedicated to that
question.


My question was actually subtler than what is addressed in the FAQ. The FAQ
item has to do with the difference between the two approaches to
constructing the Complex class. I guess I wasn't clear enough since a few
people seem to have assumed I was asking the question addressed in the
item. What I really wanted to know is what exactly does double re; re(r);
mean? I believe the answer is that all fundamental type in C++ are
actually objects. I believe he states as much in Chapter 10. I can't find
it right now. I guess Stroustrup didn't think it important enough to
warrent more than a passing comment.


Well, I don't have the book (except the first edition, long since outdated).

But here's the technical background.
(1)

When T is a type name, the expression
T( ... arguments, if any )
is a value of type T initialized with the given actual arguments as
construction arguments.

When T is a built-in type there is no actual constructor, but the
allowed syntax is _as if_ there were default constructor so that e.g.
int()
means the value 0 of type int. And also _as if_ there were a copy
constructor.

(2)

The notation '=' is a bit more difficult because it has two different
meanings.

First, in declarations '=' can be used instead of constructor notation,
when the constructor takes exactly one argument. In this situation '='
stands for initialization. Initialization is different from assignment
in that with initialization there is no prior value (e.g., no dynamically
allocated memory to be deallocated), whereas with assignment it is assumed
that there is a prior value (e.g., deallocation may have to be done).

For example, in the variable declaration
int x = 7;
'=' is a short notation for initialization, equivalent to
int x( 7 );
using the 'as if' int copy constructor.

(3)

Outside of declarations '=' means assignment, which is different from
initialization in that it is assumed that there is a prior value. For
example,
std::string s = "Hi";
is a pure initialization with no deallocation, equivalent to
std::string s( "Hi" );
whereas
std::string s;

s = "Hi";
is a default initialization (possibly with dynamic allocation) followed
by an assignment (which possibly may have to deallocate something).

Jul 22 '05 #9

"Alf P. Steinbach" <al***@start.no > wrote:

* Hattuari <su******@setid ava.kushan.aa> schriebt:
> On page 32 of TC++PL SE is an example of a complex class as follows:


Don't know since I don't have the text; presumably you could find that
out yourself?


One might also presume that I looked before asking the question.


What in the world are you talking about?


Presumably - again - he meant that he looked in the book before asking the
question, so that your advice to consult the book was not well-taken. Or
was your question rhetorical?

Best regards,

Tom

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1923
by: Christian Meier | last post by:
Hallo NG My problem is the deviation of the floating point datatypes. 90.625 is not exactly 90.625. It is 90.624999999.... on my system. Now I tried to find a solution for my problem but I couldn't think of one. So I tried to write a work-arround which should solve the problem for the most numbers. What do you think of this: (I know it is not a perfect solution....) double dValue = ...;
44
16322
by: Daniel | last post by:
I am grappling with the idea of double.Epsilon. I have written the following test: public void FuzzyDivisionTest() { double a = 0.33333d; double b = 1d / 3d; Assert.IsFalse(a == b, "Built-in == operator should not be
12
11748
by: Brett Hofer | last post by:
I must be missing something - Im a veteran C++ programmer now working with C# overall I like the language but find many weird changes... Anyway Im writing code behind an aspx. In this one C# method I am building an XML string to be insterted into a database. This string should result in: <row FIELD1="value1" FIELD2="value2" \> I am using a string type variable and I cannot get the double quotes to be added properly I have tried all of...
4
11049
by: Daniel Walzenbach | last post by:
Hi, I wonder if somebody could explain me the difference between Double.Parse and Convert.ToDouble. If I'm not mistaken they are implemented differently (I though for a moment they might be the same like cint(anInt) and cType(anInt, System.Int32) but I checked with ildasm) - if I didn't made a mistake. So when to use which syntax? Is there any performance penalty when using the one over the other or does anybody knows any differences? '...
9
3426
by: Greg Buchholz | last post by:
/* While writing a C++ version of the Mandelbrot benchmark over at the "The Great Computer Language Shootout"... http://shootout.alioth.debian.org/gp4/benchmark.php?test=mandelbrot&lang=all ....I've come across the issue that complex<double>'s seem quite slow unless compiled with -ffast-math. Of course doing that results in incorrect answers because of rounding issues. The speed difference for
9
4897
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no avail. The function receives a string from main and then inserts in the list the new word in alphabetical order. Here is my function: struct word { // double linked list. Here the list is global and is first built
1
9439
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
30
4021
by: copx | last post by:
I am writing a program which uses sqrt() a lot. A historically very slow function but I read on CPUs with SSE support this is actually fast. Problem: C's sqrt() (unlike C++ sqrt()) is defined to work on doubles only, and early versions of SSE did not support double precision. The CPU requirement "something with at least first generation SEE support" (everything from P3/Athlon XP and up) is acceptable for me. However, requiring a CPU which...
9
1692
by: AGP | last post by:
I've been scratching my head for weeks to understand why some code doesnt work for me. here is what i have: dim sVal as string = "13.2401516" dim x as double x = sVal debug.writeline ( x)
0
8367
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8279
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
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
8703
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
8467
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,...
1
6160
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
5619
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
4145
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.