but in C++ the syntax looks much different. you can't do something like
dog *Scooby = new dog("Scooby");
right? |
Yes, you can. See:
http://www.informit.com/articles/article.aspx?p=1852519
Class initialization. Oh dear, I had two lines missing from the code.
C++ class can have multiple constructors, overloaded functions. The
default constructor takes no arguments and supplies "default values" for all members. Not all classes have default constructor.
In the syntax of definition of a constructor:
1 2 3 4 5
|
Foo::Foo( /* arguments */ )
: /*initializer list*/
{
/* body of constructor */
}
|
* Different constructors of Foo differ by what arguments they take.
* All the members (both base classes and member variables) are initialized before the body of constructor is executed.
* If you do need non-default initialization of a member, it has to be done within the initializer list.
Lets be more clear about "Arrays cannot be returned from functions":
A function takes as an argument either by value or by reference. (C does not have references.)
In the by value the value is copied from caller's variable into functions (argument) variable.
In the by reference the functions argument is a reference rather than a variable.
Either way, on function call, the argument is initialized from caller's data (either copy construction or creation of a reference).
Arrays are not copied. You can have a "reference to array" argument type, but that is technique rarely seen. The usual array argument style inherits from C.
Developers of C decided not to copy, put to pass the address of array instead. A pointer. Only the address, no size. Back then copying large arrays would (1) create memory hogging copies, (2) take time, and (3) complicate the compiler. It was left to the programmer to do something smart.
1 2 3 4 5 6 7 8 9
|
void bar( int * array, int size );
int main() {
const int N = 5;
int arr[N];
// code
bar( arr, N );
// code
}
|
However, to make intent more apparent in the code, the authors of C added an alternate parameter syntax:
1 2 3
|
void bar( int [] array );
// is same as
void bar( int * array );
|
When you use the bracketed-style you hint that this function wishes to get an array rather than any pointer.
The return value of a function is again either a value (copy) or a reference. Just like in the function arguments, there is no automatic copy of every element of an array, so there is no way to get a copy of array. You can return a pointer or reference, but both of those are cause havoc, if they point to function local (stack) variable, which ceases to exists at the end of the function's scope.
Now enter the user-defined types with copy constructors. Those types are "objects". Their copy constructor can contain code to copy values from source objects array to destination objects array. The custom type can encapsulate array and perform the operations that base language does not do for plain arrays. The std::string, std::array, std::vector, etc are such custom types. They can be passed to and returned from functions
by value.
Passing an object by reference to function avoids unnecessary copying, and if the reference is also
const
, then the function cannot modify its argument (i.e. caller's variable).
The
Scoopy->message
is a pointer. It stores an address. The
"Hello from "
and
"dog bones"
are constant string literals. Both have some address. All those characters are stored somewhere in the executable and in memory during runtime, but neither in stack nor in heap.
Your code changes the address stored in
Scoopy->message
.