My C++ Quiz

The ultimate place for testing what you have learned



Home | Start Quiz | List of questions  | Next Question | Login / Register

Question #72: What gets printed?
57% on 642 times asked

#include <iostream>

struct mybase
{
  int x;
  
  template <int RANGE>
  virtual void print()
  {
    std::cout << RANGE + x + 1 << std::endl;
  }
};

struct myderived : public mybase
{
  template <int RANGE> 
  void print()
  {
    std::cout << RANGE + x + 2 << std::endl;
  }
};

int main(int argc, char** argv)
{
  mybase* b = new myderived;

  b->x = 1;

  b->print<5>();

  return 0;
}

2
6
7
8
code is ill-formed
© 2007-2010, My C++ Quiz, All rights reserved.
Can you find a bug in the quiz? Can you think of a new question that would be cool to add to this site? Email us and we will take action:
email us
Other Quizes | Contributors