Passing dynamic memory: pointer arguments


Why can't a pointer be set equal to a constant pointer?

Ex:

1
2
3
4
void do_somthing(const [type]& var)
{
    [type] *variable = &var;
}


somewhere in a function...

1
2
[type] *variable = new [type]();
do_somthing(*variable);


This gives me the error:

(this is just the generalized message...)
error: invalid conversion from 'const [type]* {aka const [structure]*}' to '[type]* {aka [structure]*}' [-fpermissive]


EDIT: NEVER MIND. I feel stupid, I just realized it creates read-only memory... and I call functions that modify the information at the address....

ok... next:

Is there a way for me to... pass the pointer without creating any new memory; i.e. passing the... (address?) of the pointer... or somthing to that effect??
Last edited on
You could have a function accept a pointer to a pointer
1
2
3
4
void do_somthing(const [type]** var)
{
    [type] *var = something;  //also read as: value pointed to by var = something
}

Somewhere else:
1
2
[type] *variable;
do_somthing(&variable); //also read as: do_something(address of variable) 
You could pass a unique_ptr or shared_ptr by reference. http://ideone.com/UYzXn2
Alright. Thanks.
Topic archived. No new replies allowed.