Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: explain this function declaration

class PageNavigator {
 public:
  // Opens a URL with the given disposition.  The transition specifies how this
  // navigation should be recorded in the history system (for example, typed).
  virtual void OpenURL(const GURL& url, const GURL& referrer,
                       WindowOpenDisposition disposition,
                       PageTransition::Type transition) = 0;
};

I don't understand what is that =0; part...what are we trying to communicate?

like image 807
deostroll Avatar asked Aug 16 '26 04:08

deostroll


1 Answers

'= 0' means it's a pure virtual method. It must be overriden in inheriting class.

If a class has a pure virtual method it is considered abstract. Instances (objects) of abstract classes cannot be created. They are intended to be used as base classes only.

Curious detail: '= 0' doesn't mean method has no definition (no body). You can still provide method body, e.g.:

class A
{
 public:
  virtual void f() = 0;
  virtual ~A() {}
};

void A::f()
{
  std::cout << "This is A::f.\n";
}

class B : public A
{
 public:
  void f();
}

void B::f()
{
  A::f();
  std::cout << "And this is B::f.\n";
}
like image 69
Tomek Szpakowicz Avatar answered Aug 18 '26 20:08

Tomek Szpakowicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!