2
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?

2
  • 2
    I see no definition of a function. I see a declaration of a pure virtual function. Pure due to the =0; syntax. Classes with such functions are automatically abstract and cannot be instantiated. They can only be used as base classes. Instantiation requires you to override those functions in some derived class. Commented Jun 23, 2010 at 12:02
  • noted the change and edited the title...thanx Commented Jun 23, 2010 at 12:08

3 Answers 3

10

'= 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";
}
Sign up to request clarification or add additional context in comments.

10 Comments

doesn't the keyword virtual already say that? Or do you mean its an empty function: equivalent to replacing =0; with { }
No, the =0; means that there's explicitly no implementation defined. As a consequence you cannot instantiate this class. Instead, you need to derive a subclass and implement this method in the subclass then instantiate that.
ok so having { } means you can instantiate that class and call the OpenURL() method?
@deostroll: virtual functions without =0 need to have implementation and don't have to be overridden in derived classes. virtual fcuntions with =0 don't have to (but can) have implementation and need to be overridden in derived classes.
You need to instantiate a subclass, because an abstract class can't be instantiated.
|
3

It's a pure virtual function - there's no definition in the base class, making this an abstract class, and any instantiable class that inherits from PageNavigator must define this function.

Comments

0

The = 0 means the function is a pure virtual or abstract function, which in practice means two things:

a) A class with an abstract function is an abstract class. You cannot instantiate an abstract class.

b) You have to define a subclass which overrides it with an implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.