class Car
{
public:
Car(string name, const int wheels = 4){}
private:
string name;
};
void main()
{
Car("Ford",3); // Why can I put in a 3?
}
pac-man
0
Junior Poster in Training
Recommended Answers
Jump to PostIn addition to that, even if you initialize a constant variable in your constructor, the value to be used is the value passed in when an object of Car is instantiated. That's why 3 is accepted as the value for wheels.
However, if you try to compile this code:
Jump to PostTo my knowledge, only if wheels is a member variable declared with keyword const. That is, it is the declaraion the member variable called wheels as const that prevents it from being changed somehow after construction. If keyword const is left off of the declaration of wheels in post #3 …
All 5 Replies
Lerner
582
Nearly a Posting Maven
tkud
0
Posting Whiz in Training
pac-man
0
Junior Poster in Training
Lerner
582
Nearly a Posting Maven
pac-man
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.