pointers and references

In the program below, we copy a reference from cDerived to rBase with "Base &rBase = &cDerived;" But why doesn't it use a pointer instead? Wouldn't it make more sense to do the following: "Base *rBase = &cDerived;" since a pointer is what points to an address.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Base
{
protected:
 
public:
    virtual const char* GetName() { return "Base"; }
};
 
class Derived: public Base
{
public:
    virtual const char* GetName() { return "Derived"; }
};
 
int main()
{
    Derived cDerived;
    Base &rBase = &cDerived;
    cout << "rBase is a " << rBase.GetName() << endl;
 
    return 0;
}

# output:
rBase is a Derived
In function ‘int main()’:
18: error: invalid initialization of non-const reference of type ‘Base&’ from an rvalue of type ‘Derived*’


1
2
Base &reference = cDerived;
Base *pointer = &cDerived;
Topic archived. No new replies allowed.