943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 564
  • C++ RSS
Apr 28th, 2009
0

how to define a constructor with const members

Expand Post »
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
cpp Syntax (Toggle Plain Text)
  1. Bike bicycle (2, "Monark", "blue", 24.11);

and here is the constructor code
cpp Syntax (Toggle Plain Text)
  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

cpp Syntax (Toggle Plain Text)
  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. };
Similar Threads
Reputation Points: 46
Solved Threads: 1
Light Poster
ganmo is offline Offline
38 posts
since Mar 2009
Apr 28th, 2009
0

Re: how to define a constructor with const members

Try this:
cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 28th, 2009
0

Re: how to define a constructor with const members

Bad Post
Last edited by MatEpp; Apr 28th, 2009 at 3:31 pm. Reason: My apologies, I posted false information.
Reputation Points: 21
Solved Threads: 12
Junior Poster in Training
MatEpp is offline Offline
79 posts
since Jan 2009
Apr 28th, 2009
0

Re: how to define a constructor with const members

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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Apr 28th, 2009
0

Re: how to define a constructor with const members

thanks, it works now
Reputation Points: 46
Solved Threads: 1
Light Poster
ganmo is offline Offline
38 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: conversion into class
Next Thread in C++ Forum Timeline: LocalApplicationData Folder





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC