Can we just use reference in C++ when I dont need change location of pointer? The purpose is to simulate process of Java. For example:
//declare an object
Foo *f = new Foo;
Foo &rf = *f;
//pass parameter
void foo(Foo &f){}
foo(rf);
I want to avoid using class variable created in stack.
I want to avoid using class variable created in stack.
Why? Using automatic storage duration whenever possible is the best way to do it because it makes your code more clean and you don't have to remember deleting the object.
std::string, std::vector and many other containers use dynamic allocations internally so they will not take up much space on the stack. An image class will work the same way. If you try to make an image class without dynamically allocating the pixel data you would have to store a large fixed size array in the object which is not so good because images often have various sizes.