0

I'm reading that: http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/

// Example 3

Derived factory(); // construct a Derived object

void g() {
  const Base& b = factory(); // calls Derived::Derived here
  // … use b …
} // calls Derived::~Derived directly here — not Base::~Base + virtual dispatch!

and I understand first and second example, but I dont undetstand 3rd example. Particullary, what is lreference/rreference and what is 'virutal dispatch'. The example would be great.

  • What has googling "C++ virtual dispatch" told you? The article doesn't contain the word "lreference" or "rreference." It does say "rvalue reference," however. What has googling this told you? What particular pieces don't you understand? – Angew is no longer proud of SO Apr 28 '14 at 13:53
  • It seems you are a bit too ahead reading gotw, that discuss fine points and advanced C++ techniques. I recommend going through some [good general textbook](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first. You may also consider online [C++ Annotations](http://www.icce.rug.nl/documents/cplusplus/). – Jan Hudec Apr 28 '14 at 13:56
  • @JanHudec Insulting the questioner is not a helpful comment. The whole point of the (very good!) question is the OP is reading gotw trying to learn. Most "general textbooks" won't mention this somewhat esoteric (but still important) point. – Dale Wilson Apr 28 '14 at 14:14
  • 1
    You're asking like three different questions about basic terms in C++. You should do some research first. – Lightness Races in Orbit Apr 28 '14 at 14:28

1 Answers1

1

If you write code like below:

void g() {
  Base* b = new Derived; // calls Derived::Derived here
  // … use b …
  delete b;
}

Because b is of type Base *, still if base destructor was virtual, because of virtual dispatch Derived destructor would be called because object pointed by base pointer has type derived.

Instead if you use

void g() {
  Derived* d = new Derived; // calls Derived::Derived here
  // … use d …
  delete d;
}

No matter whether base destructor was virtual or not, derived destructor is called. No virtual dispatch is necessary.

Writer want to emphasize that in Example 3, even if base destructor is not virtual, derived destructor would be called. Because destructor is not being called because reference is going out of scope but because temporary object (of type derived) is not needed any more by the base reference. (must be const)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100