5.8 - Function Declaration vs Array Declaration SYNTAX CLARIFICATION

this declares an array:

string word2("Over");
string word3(3, '!');

how are these not being mistaken for functions that pass default parameters? Is it because the parameters placed above are not = to anything or being defined as a type? Are the following valid function delcaration equivalents:

string word2(string = "Over");
string word3(char = 3, char = !);

Recommended Answers

All 3 Replies

>>string word2("Over");
>>string word3(3, '!');

The string class, has the proper constructor that allows those syntax. So it calls
the string class constructor. Other that that, i'm not so sure about your question, maybe its because i'm tired.

When you call

>>string word2("Over");

the string class detects that you passed an array of characters to it and uses that to create word2.

When you call

>>string word3(3, '!');

the string class detects that you have passed an integer as first parameter and a character as second, it creates a string that looks like this "!!!".

Once you learn about classes and constructors you will understand how the string class decides what to do with the arguments you give it.

I get what you're saying but yeah I guess we haven't covered that yet. Thanks for helping! Now when we get there, I'll be prepped to soak up all the juicy details of classes.

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.