473,327 Members | 2,071 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,327 software developers and data experts.

constructor starting ..Demo &operator=(double x) {.....}

I am a beginner here is the extract from a text book. The
"???" next to the coding extract(line 12) which I wish someones could give me a simple explaination:

Expand|Select|Wrap|Line Numbers
  1. // Store a class object in a vector.
  2.       #include<iostream>
  3.       #include<vector>
  4.       using namespace std;
  5.  
  6.       class Demo {
  7.          double x;
  8.       public:
  9.          Demo() { d = 0.0; }
  10.          Demo(double x) { d = x; }
  11.  
  12.   (???)Demo &operator=(double x){
  13.             d = x; return *this;}
  14.  
  15.           double getd() { return d; }
  16.        };
  17.  
  18.        bool operator<(Demo a, Demo b)
  19.        {
  20.           return a.getd() < b.getd();
  21.        }
  22.  
  23.        bool operator==(Demo a, Demo b)
  24.        {
  25.           return a.getd() == b.getd();
  26.        }
  27.  
  28.        int main()
  29.        {
  30.           vector<Demo> v;
  31.           int i;
  32.  
  33.           for(i=0; i<10; i++)
  34.              v.push_back(Demo(i/3.0));
  35.  
  36.           for(i=0; i<v.size(); i++)
  37.              cout << v[i].getd() << " ";
  38.  
  39.           cout << endl;
  40.  
  41.           for(i=0; i<v.size(); i++)
  42.              v[i] = v[i].getd() * 2.1;
  43.  
  44.           for(i=0; i<v.size(); i++)
  45.              cout << v[i].getd() << " ";
  46.  
  47.           return 0;
  48. }
  49.  
Nov 16 '13 #1
1 1500
weaknessforcats
9,208 Expert Mod 8TB
The function is operator=, which is the assignment operator, and it is supposed to assign the double argument x to the class member d.

Too bad there is no class member d. There is a class member x so I suspect the argument should have been d and assign that to the class member x. In other words, this is buggy code.

In addition the operator= returns a Demo&. This is really bad since the Demo& can be used to change the this object. Therefore, the operator= should be returning a const Demo&.

Lastly, an assignment operator iis to assign one Demo object to another. That means the argument needs to be a Demo& and not a double. A double argument would be used on a separate member function to change only the the Demo::x member.

I suggest you get a better book.
Nov 22 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Doug Farrell | last post by:
Hi all, I just installed the wxPython-2.4.1.2 demo RPM on my RedHat Linux 9.0 machine, which has Python 2.2.2 on it, and I'm having a problem running the demo.py program. Here is a trace of the...
1
by: John Kinson | last post by:
Hi, I'm trying to get the wxPython demo to run on a Debian sarge testing installation, but encounter the following error when I run the demo script: # ./demo.py Traceback (most recent call...
1
by: Dave Theese | last post by:
Hello all, Please see the two questions and output embedded in the program below. Thank you! Dave #ifdef WIN32 #pragma warning(disable: 4786)
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
4
by: Jerry Krinock | last post by:
I've written the following demo to help me understand a problem I'm having in a larger program. The "main" function constructs a Foo object, and then later "reconstructs" it by calling the...
19
by: chee.k.cheng | last post by:
Hi, I'm wondering if it is possible to change the starting address of a structure element. For example, I have the following structure: struct demo { char digit; char alpha; } an;
9
by: Thomas Scheiderich | last post by:
I am curious as to 2 things. One is the order that the constructors are handled and the other is why the debugger doesn't show the static objects constructor (unless you set a breakpoint. For...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
1
by: Frank Rizzo | last post by:
Hello, I am supposed to give a demo on how to become a programmer to complete newbies. Can anyone suggest a C# project that I can download and explain it to people within 30-40 minutes? Most...
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
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.