how to define a constructor with const members

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 34
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

how to define a constructor with const members

 
0
  #1
Apr 28th, 2009
Hello, I am having problem to define a constuctor of a class with private member which is also of a const type. When I try to compile it complains that the const members do not have initalized data. Hmm, anyway it works fine if I remove const. However is it possible to define a constructor with const members which is possible to set values to?

I know that const is values you can't change values on, but exercise says to use const. help is appreciated.

e.g. I want to create an object of a person
  1. Bike bicycle (2, "Monark", "blue", 24.11);

and here is the constructor code
  1. Bike::Bike(const int wheel, const string brand, string color, double wheelsize) {
  2. w = wheel;
  3. b = brand;
  4. c = color;
  5. ws = wheelsize;
  6.  
  7. }

and here is the class file

  1. class Bike {
  2. public:
  3. Bike (const int, const string, string, double);
  4. private:
  5. const int w;
  6. const string b;
  7. string c;
  8. double ws;
  9. };
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: how to define a constructor with const members

 
0
  #2
Apr 28th, 2009
Try this:
  1. class Bike {
  2. public:
  3. Bike (const int, const string, string, double);
  4. private:
  5. const int w;
  6. const string b;
  7. string c;
  8. double ws;
  9. };
  10. Bike::Bike(const int wheel, const string brand, string color, double wheelsize):
  11. w(wheel),
  12. b(brand),
  13. c(color),
  14. ws(wheelsize)
  15. {}
It is generally better to write constructor using member initialization(what I did) rather than assignment(what you did).
This approach is more efficient.
why? because in this approach you are saving the overhead to create wheel,brand,color,wheelsize. Instead you are just using those value just to initialize your data members
Last edited by siddhant3s; Apr 28th, 2009 at 3:23 pm.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 79
Reputation: MatEpp is an unknown quantity at this point 
Solved Threads: 12
MatEpp MatEpp is offline Offline
Junior Poster in Training

Re: how to define a constructor with const members

 
0
  #3
Apr 28th, 2009
Bad Post
Last edited by MatEpp; Apr 28th, 2009 at 3:31 pm. Reason: My apologies, I posted false information.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 792
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: how to define a constructor with const members

 
0
  #4
Apr 28th, 2009
MatEpp>>The names, wheel, brand, etc. are missing from
[edit]This was posted in the post above. But Maybe he realised his fault and then removed this remark marking it as "Bad Post"[/edit]
That is a prototype of the constructor. It is perfectly fine.
It just tells the compiler that " you will find the definition of the constructor somewhere in the code below".
You usually omit the name of the formal parameters when declaring prototype.
Last edited by siddhant3s; Apr 28th, 2009 at 3:35 pm.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 34
Reputation: ganmo is on a distinguished road 
Solved Threads: 0
ganmo ganmo is offline Offline
Light Poster

Re: how to define a constructor with const members

 
0
  #5
Apr 28th, 2009
thanks, it works now
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC