0

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 = !);

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by crapgarden
1

>>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.

1

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.

Edited by Andreas5: n/a

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.