What are some of the disadvantages of using a link instead of a pointer?

Given the class "A" exists and is correct. What will be some negative results of using the reference to "A" instead of the pointer in the class "B". I.e:

// In Declaration File
class A;

class B
{
public:
   B();
   ~B();
private:
    A& a;
};

// In Definition File
B::B(): a(* new A())
{}

B::~B()
{
    delete &a;
}

Missing additional code for further B correctness, such as the copy constructor and assignment operator, simply wanted to demonstrate the concept of the question.

+5
source share
6 answers

Immediate restrictions are as follows:

  • . A, , A B .
  • A 0.

:

  • .
  • B , A .
  • B , . B, , std::vector<B*>, ( , ).

, .

:

  • slicing - , A, B.
+4

, a , . . , -POD ( -POD - , ).

, , .

+2

, B, , ; a.

, , , delete &a . , new, , , , , B.

+1

:

  • ,
  • null

, , , - (std:: unique_ptr, std:: shared_ptr ..). .

+1

; , , , , . , .

0

, , , , , , , , , , . , ,

0

All Articles