What does this line of code mean?

In this phrase : " virtual Item* add(Item *item) = 0; "
What does it mean to put Item* before the function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  class Item
{
public:
	// prints data
	virtual void print() = 0; 

	// returns “INTEGER” or “STRING”
	virtual string getType() = 0; 

        // Adds or concatenates the 2 data members. 
        // Throws an exception in case of a type mismacth 
	virtual Item* add(Item *item) = 0; 
};
Hi,

That is the return value. It is a pointer to an Item type.

The other functions return a string or nothing (void)

Hope this helps.
Yeah but normally we use Item for constructors and destructors.
Can we use Item* to return what exactly? An address to the pointer?
I think i understood thanks a lot!
I believe it goes like this:
(Assuming not an abstract class and a class for integers or something)

Item *a = new Item(1);
Item *b = new Item(2);

b=b->add(a);

b becomes 3.
Last edited on
(Assuming not an abstract class and a class for integers or something)


But it is an abstract class, the functions are pure virtual (because of the = 0 at the end), so it needs to be over ridden in a derived class. If any function is pure virtual, then it's an abstract class. And one specifically cannot create an instance of Item

The class does need a virtual destructor as well, whenever there are virtual functions you need a virtual destructor.

The idea of returning a pointer to the base class is probably meant to achieve polymorphism - have a read in the tutorial at the top left of this page - it explains it well :+)

It has nothing to do with constructors, the name of the function is add, whereas the class name is Item.

Avoid using new and delete, use RAII with smart pointers instead. Read up about that also :+)

There is some really good reading here, by The inventor of C++ Bjarne Stroustrup, and Herb Sutter:

http://www.cplusplus.com/forum/lounge/174163/


Thanks to MiiNiPaa for posting that, it was awesome :+D
Thanks!
Topic archived. No new replies allowed.