1

Im confused what virtual int and virtual string means.

Moreover, I'm confused what the prefix "virtual" has to do with anything; I have a feeling it has to do with vectors.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
alexyorke
  • 4,224
  • 3
  • 34
  • 55
  • 1
    `virtual` in C++ normally refers to member functions, and their ability that they are dispatched to the most derived type when invoked on a reference to a base class. – Xeo Jan 01 '12 at 02:23
  • 3
    have you read a [book about C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Sam Miller Jan 01 '12 at 02:23
  • 2
    Please show the code that caused your confusion: there are two places where `virtual` can be used in C++, and they are very different. – Sergey Kalinichenko Jan 01 '12 at 02:25
  • Unless you have worked with inheritance, you really don't need to know anything about virtual. You can get to that when you get there, until then, don't even bother trying with it. – Serodis Jan 01 '12 at 02:26
  • I understand what it means now, thank you Xeo. If you make your comment an answer I'll mark it as correct. – alexyorke Jan 01 '12 at 02:27
  • @dasblinkenlight: Not if there's an `int` after it and the OP isn't getting a compiler error. There's only one place where virtual could be used like that. – Omnifarious Jan 01 '12 at 02:28
  • alexy, @Xeo isn't guaranteed to see your reply to his comment unless you prefix his name with @. – 01d55 Jan 01 '12 at 02:36
  • Thanks for noticing that, I forgot about it. I'll make a new comment since I can't edit my last one. @Xeo – alexyorke Jan 01 '12 at 02:37
  • Don't worry about that. :) Just accept another answer, @Omnifarious' in particular looks good. – Xeo Jan 01 '12 at 02:57

3 Answers3

3

The virtual has nothing to do with the int, nor does it have anything to do with vectors. The int specifies the type of value the function returns when it's done. The virtual specifies that the function call be handled in a special way that's related to inheritance.

I expect this question has duplicate answers on here already. Search for stuff about the virtual keyword in C++.

Your question is a very elementary C++ question, and I believe a full explanation of what virtual means is outside the scope of what StackOverflow can help you with anyway. You should find a nice beginners C++ book.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
1

virtual describes a class member function in most cases.

If you see virtual int some_function() it's the function that's virtual.

You can learn about the concept here.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
0

In C++ "virtual" is a keyword used in a class' or struct's method declarations that makes the function polymorphic based on the object type. You can read up on the basic, language-independent concept at http://en.wikipedia.org/wiki/Virtual_function, but you'd be best served reading an introduction C++ book to learn how this is used.

wjl
  • 7,519
  • 2
  • 32
  • 41