how to define a virtual to_string method in C++

I want to make a virtual to_string method for a base class called Item which is the base class for the derived class Book.


In Item I have several fields:

private:
int isbn_;
string title_;
bool status_ = false;
string borrower_;

public:
using pointer_type = shared_ptr<Item>;
using string_type = std::string;


I tried to define the to_string function in many different ways. All of them have failed. My different approaches are commented out in the virtual to_string function.


I tried this:

std::ostringstream oss; --> but this is giving me incomplete type is not allowed.



virtual string_type to_string()
{
std::ostringstream oss;
//return string_type("isbn: " + isbn_) + string_type("Title " + title_) + string_type(" Status " + status_) + string_type(" Borrower " + borrower_);

//return "isbn: " + to_string(isbn_);

//+ to_string("Title " + title_) + to_string(" Status " + status_) + string_type(" Borrower " + borrower_);

}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

class Item {

private:

	int isbn_;
	string title_;
	bool status_ = false;
	string borrower_;

public:
	using pointer_type = shared_ptr<Item>;
	using string_type = std::string; 

	Item(int isbn, string title, bool status, string borrower) : isbn_(isbn), title_(title), status_(status), borrower_(borrower) {}

	bool getstatus()
	{
		return status_;	
	}

	string getborrower()
	{
		return borrower_;
	}

	void setstatus(bool status)
	{
		status_ = status; 
	
	}

	void setborrower(string borrower)
	{
		borrower_ = borrower ;

	}


	virtual void checkout(string borrower)
	{
	};

	virtual string_type to_string()
	{
		std::ostringstream oss;
		//return string_type("isbn: " + isbn_) + string_type("Title " + title_) + string_type(" Status " + status_) + string_type(" Borrower " + borrower_);

		//return "isbn: " + to_string(isbn_);
			
			//+ to_string("Title " + title_) + to_string(" Status " + status_) + string_type(" Borrower " + borrower_);

	}

	virtual ~Item(){}



};
Did you #include <sstream> ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <string>
#include <memory>

class Item {

    public:
        using pointer_type = std::shared_ptr<Item>;
        using string_type = std::string;

    private:

        // int isbn_;
        string_type isbn_ ; // shouldn't this be a string?
        string_type title_;
        bool status_ = false;
        string_type borrower_;

    public:

        Item( string_type isbn, string_type title, bool status = false, string_type borrower = {} )
        : isbn_( std::move(isbn) ), title_( std::move(title) ), status_(status), borrower_( std::move(borrower) ) {}

        virtual ~Item() = default ;

        Item( const Item& ) = default ;
        Item( Item&& ) = default ;
        Item& operator= ( const Item& ) = default ;
        Item& operator= ( Item&& ) = default ;

        virtual string_type to_string() const // *** const
        {
            return "isbn: " + isbn_ + ", title: '" + title_ + "', status: " +
                   ( status_ ? "true" : "false" ) + ", borrower: '" + borrower_ + "'" ;
        }

        virtual std::ostream& print_to( std::ostream& stm ) const
        { return stm << type_name() + "{ " << to_string() << " }" ; }

    protected: virtual string_type type_name() const { return "Item" ; }

    friend std::ostream& operator<< ( std::ostream& stm, const Item& it )
    { return it.print_to(stm) ; }
};

struct Book : Item {

    public:
        Book( string_type isbn, string_type title, string_type author,
              bool status = false, string_type borrower = {} )
            : Item( std::move(isbn), std::move(title), status, std::move(borrower) ),
              author_( std::move(author) ) {}

        virtual string_type to_string() const override
        { return Item::to_string() + ", author: '" + author_ + "'" ; }

    protected: virtual string_type type_name() const override { return "Book" ; }

    private: string_type author_ ;
};

int main()
{
    Item a( "978-1491903995", "Effective Modern C++" ) ;
    Book b( "978-0321992789", "PPP-2", "Stroustrup", true, "masterinex" ) ;

    std::cout << a << '\n' << b << '\n' ;
}

http://coliru.stacked-crooked.com/a/875cfd52d5092cb2
I'd think again before doing so, as it creates some high complexity within the code. Complexity makes errors very hard to be detected and make you spend a lot of time on it. Unless you use one of those programs, such as checkmarx that helps with it, I'd consider things again.
Good luck.
Topic archived. No new replies allowed.