c++ - How to assign a variable a new object -
c++ - How to assign a variable a new object -
let's have line of code this:
myclass obj(1, 2, "hello");
and @ point in future want assign obj
new value, i.e. obj(3, 4, "bye")
. how this?
i mean in java this:
myclass obj = new myclass(1, 2, "hello"); obj = new myclass(3, 4, "bye");
in c++ have tried this:
myclass obj(1, 2, "hello"); obj(3, 4, "bye");
but didn't work, have tried this:
myclass obj(1, 2, "hello"); obj = myclass(3, 4, "bye");
and did work, not sure if preferred way accomplish this.
please suggest me clean way assign variable new object , tell me if there additional effort needed manage memory after such assignment.
please maintain in mind have started learning c++ 2 days ago, know oop concepts know how many fingers hands have.
the code in c++ similar java code
myclass obj = new myclass(1, 2, "hello"); obj = new myclass(3, 4, "bye");
will like
myclass *obj = new myclass(1, 2, "hello"); delete obj; obj = new myclass(3, 4, "bye");
the way using assigning object in c++ like
myclass obj(1, 2, "hello"); obj = myclass(3, 4, "bye");
is valid provided there valid re-create or move assignment operator defined in class.
c++ variables object constructor variable-assignment
Comments
Post a Comment