First he is using C not C++ it's NOT possible to do that in C.
C++ == OOP
C == non OOP
the new and delete operators are introduced in C++ and are for C++ only.
As for allocating memory you should always CLEAR allocated memory (otherwise you could have a mess on your hands). Use calloc instead of malloc. You can use it identically as malloc.
Don't forget to free your memory after allocating it (the biggest error of most green programers ).
Other things
delete k will work for BOTH objects in your C++ code. Older (non compliant to ANSI C++) compilers may require the [] placed in front of the k. As I said these are not ANSI compliant though.
you use the same destructor syntax for an array of objects
as you do a single object
Of course then there are object factories.. that's a very different horse but the syntax is somewhat the same. An object factory has a generic base type that creates the correct object needed.
Container *Box;
Box = new Container("SoapBox");
This creates a soap box. It doesn't seem special until you realize that a type of SoapBox is a descendant of type container. All these types can be assigned to a Container type and the base class functions are all accessable.
Cyb
C++ == OOP
C == non OOP
the new and delete operators are introduced in C++ and are for C++ only.
As for allocating memory you should always CLEAR allocated memory (otherwise you could have a mess on your hands). Use calloc instead of malloc. You can use it identically as malloc.
Don't forget to free your memory after allocating it (the biggest error of most green programers ).
Other things
delete k will work for BOTH objects in your C++ code. Older (non compliant to ANSI C++) compilers may require the [] placed in front of the k. As I said these are not ANSI compliant though.
you use the same destructor syntax for an array of objects
Code:
K = new char[144];
delete K;
Code:
K = new char;
delete K;
Of course then there are object factories.. that's a very different horse but the syntax is somewhat the same. An object factory has a generic base type that creates the correct object needed.
Container *Box;
Box = new Container("SoapBox");
This creates a soap box. It doesn't seem special until you realize that a type of SoapBox is a descendant of type container. All these types can be assigned to a Container type and the base class functions are all accessable.
Cyb