You are about right, if I understand correctly what you mean by
"anonymous", and if you want the object to stay alive for longer term.
This is because in C++ the object lifetimes are deterministic. Either
you create the object on stack (automatic storage duration) and then it
needs a named object or at least a const reference to keep it alive, or
alternatively, you create an object via new or equivalent (dynamic
storage duration) and then you need a pointer for accessing and finally
destroying the object.
The nearest you can get to "a non-newed object and pointer" is "a
non-newed object and a const reference". There is a special rule that a
const reference keeps a temporary object alive during the lifetime of
the reference.
#include<iostream>
class parent{
public:
parent():_something{nullptr}, _val{0} {
std::cout << "null constructor\n";
};
parent(parent *in):_something{in}, _val{10} {
std::cout << "here\n";
_something->_val = 0;
};
parent * _something;
int _val;
};
void discover(const parent * in){
std::cout<< in->_val << std::endl;
std::cout<< in->_something->_val << std::endl;
}
int main()
{
parent * tmp = new parent;
const parent& tmp2 = parent {tmp};
discover(&tmp2);
return 0;
}