That's because it doesn't do anything. It may work as in - it needn't crash.
Let's add some flesh:
Code:
#include <iostream>
#include <string>
class A{
public:
A(int n): s("Hello world"), x(n)
{std::cout << "Constructor: " << x << '\n';}
~A() {std::cout << "Destructor: " << x << '\n';}
std::string s;
int x;
};
class B: public A{
public:
B(int n): A(n){}
};
void Test(){
const A &a = B(1);
std::cout << a.s << '\n';
A *p = &B(2);
std::cout << p->s << '\n';
}
int main(){
Test();
return 1;
}
This compiles with a warning: [Warning] taking address of temporary
My output:
Code:
Constructor: 1
Hello world
Constructor: 2
Destructor: 2
Destructor: 1
See that it failed to print "hello world" the second time (could have crashed at that place).
The const reference thing is probably there to allow you to call functions with temporary objects:
Code:
void foo(const MyClass& m) {}
...
foo(MyClass(parameters));
If you want to have a pointer to the base class, see my previous post.