There is a one line syntax to create an instance and pointer to it, in the heap allocation. Is there one line syntax for the same purpose but with stack allocation?
#include <iostream>
class Base {};
int main()
{
//Base* ptr = new Base(); // heap
Base base;
Base* ptr = &base; // stack
return 0;
}
I have no problem to use 2 lines, just thought maybe there is a special syntax for this case (I'm moving from Python :D)
Base*anyway?Base base, *ptr = &base;. It's not possible to eliminate the two parts entirely, since both variables (baseandptr) have names. BTW - terms like "heap" and "stack" are not actually part of C++ (in this context) - heap and stack are specific concepts from some implementations (particular compilers and host systems) but not relevant to all implementations.&baseto a function. You can write your function to take a reference and just passbaseitself. That's why I asked about the actual use case, because it's not clear when you'd want this.