S
soni29
hi,
i'm reading a c++ book and noticed that the author seems to allocate
memory differently when using classes, he writes:
(assuming a class called CBox exists, with member function size()):
// first way
CBox x;
x.size();
// second way
CBox x = new CBox();
x.size();
// third way
CBox* x = new CBox();
x->size();
if i wrote sometime incorrectly, i apologize, i don't have the book in
front of me, my question was is there a difference among them, or is
it just a matter of preference? also can i allocate memory
dynamically for a char[] without using a pointer, or only when using a
pointer, like:
char* ptr = new char[some_variable];
where some_variable is just user input taken in during program
execution.
Thank you.
i'm reading a c++ book and noticed that the author seems to allocate
memory differently when using classes, he writes:
(assuming a class called CBox exists, with member function size()):
// first way
CBox x;
x.size();
// second way
CBox x = new CBox();
x.size();
// third way
CBox* x = new CBox();
x->size();
if i wrote sometime incorrectly, i apologize, i don't have the book in
front of me, my question was is there a difference among them, or is
it just a matter of preference? also can i allocate memory
dynamically for a char[] without using a pointer, or only when using a
pointer, like:
char* ptr = new char[some_variable];
where some_variable is just user input taken in during program
execution.
Thank you.