Q:

why passing in a temp variable into const ref in ctor work?

// Example program
#include <iostream>
#include <string>

struct foo
{
    int d_member;

    foo(const int & in):
    d_member(in){};
};

int main()
{
    foo *p;

    {
        int temp = 5;
        p = new foo(temp);
    }

    std::cout << p->d_member << std::endl;

    return 0;
}

Foo takes a const ref in it's ctor to a int, and I passed a temporary int to it.
temp is destroyed after new. Why I am still able to print out d_member?

A:

Your constructor initialization list copies the integer argument to the member variable.  The lifetime of your member variable is the lifetime of your object.

A:

As others already mentioned with a non reference member variable your constructor call simply copies the value from the const reference parameter (and it even doesn't matter if you pass it by reference or value).
You would have a different situation, if your member variable is a reference like
struct foo
{
    const int& d_member; // <<<< reference member variable

    foo(const int & in): d_member(in){};
};

In that case, accessing d_member after your scope block would raise undefined behavior:
{
    int temp = 5;
    p = new foo(temp);
}

std::cout << p->d_member << std::endl; // <<<<  Undefined behavior

A:

Yes, temp is destroyed, but before that it was copied during d_member(in) initialization. So nothing is wrong here

