C++ Create a class instance and pointer to it in one line

279 Views Asked by At

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)

1

There are 1 best solutions below

1
On
class Base {};

Base base, *ptr = &base;

But I wouldn't consider it "a well formatted code".