| | |
how to define a constructor with const members
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 34
Reputation:
Solved Threads: 0
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
and here is the constructor code
and here is the class file
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)
Bike bicycle (2, "Monark", "blue", 24.11);
and here is the constructor code
cpp Syntax (Toggle Plain Text)
Bike::Bike(const int wheel, const string brand, string color, double wheelsize) { w = wheel; b = brand; c = color; ws = wheelsize; }
and here is the class file
cpp Syntax (Toggle Plain Text)
class Bike { public: Bike (const int, const string, string, double); private: const int w; const string b; string c; double ws; };
Try this:
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
cpp Syntax (Toggle Plain Text)
class Bike { public: Bike (const int, const string, string, double); private: const int w; const string b; string c; double ws; }; Bike::Bike(const int wheel, const string brand, string color, double wheelsize): w(wheel), b(brand), c(color), ws(wheelsize) {}
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
(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
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.
[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
(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
![]() |
Similar Threads
- Dynamically Allocated 2D Matrix Memory Issues (C++)
- Problem accessing private members and overloading operators (C++)
- Need help with class constructor.. (C++)
- traversing through a link list backward (C++)
- copy constructor & assignment operator overload problems (C++)
- HELP! Bad Programmer! (C++)
- Class Constructor Shorthand (C++)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: conversion into class
- Next Thread: LocalApplicationData Folder
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





