Martin Dvoracek
Martin Dvoracek

Reputation: 1738

Overloaded << operator for ostream, used in virtual functions

I've overloaded << opreator this way:

ostream& operator<<(ostream&os, const Computer& c) {
     for (int i = 0; i != c.listOfComponents.size() - 1; i++) {
        os << c.listOfComponents[i].getInfo(os) << endl;    
    }
    return os;
}

where listoOfComponents is vector<Component>.

My Component class and one of the child classes are here:

class Component {
public:

    Component() {
    };

    ~Component() {
    };

  virtual ostream &getInfo(ostream& os)const;
};

ostream &Component::getInfo(ostream& os)const {
    return os;
}

class CPU : public Component {
public:

    CCPU(int cores, int freq) {
        this->cores = cores;
        this->freq = freq;
    };

    ~CPU() {
    };

    virtual ostream &getInfo(ostream& os)const;

    int cores;
    int freq;
};

ostream &CPU::getInfo(ostream& os)const {
        os<<"CORES:"<<cores<<" FREQ:"<<freq<<endl;
 }

And finally the Computer class:

class Computer {
public:
    // constructor

    Computer(string name) {
        this->name = name;
    };
    // destructor

    ~Computer() {

    };


    // operator <<
    friend ostream& operator<<(ostream& os, const CComputer& c);

    CComputer & AddComponent(Component const & component) {
        this->listOfComponents.push_back(component);
        return *this;
    };

    CComputer & AddAddress(const string & address) {
        this->address = address;
        return *this;
    };

    string name;
    string address;
    vector<Component> listOfComponents;
};

But then, when I want to print it out via cout<<os; it prints out only the address (ie 0x6c180484). But I can't figure out, how to write it to be able to compile it and to get right values...

Upvotes: 0

Views: 406

Answers (3)

Joker_vD
Joker_vD

Reputation: 3765

First of all, why is the method that prints to a stream called get_info? Call it put_info() (with the same return type/parameter) and use it like

c.listOfComponents.put_info(os) << endl;

Don't forget to return the stream from put_info.

And after you do this, it still won't work, because vector<Component> holds precisely Components — if you pushes a CPU in, it's brutally truncated down to Component.

Upvotes: 1

David G
David G

Reputation: 96810

This:

os << c.listOfComponents[i].getInfo(os) << endl;

should be:

c.listOfComponents[i].getInfo(os) << endl;

This is of course assuming os returns the std::ostream object.


With the way you had it, you were printing a pointer, which returns its address (in hexadecimal).

Upvotes: 1

Ferdinand Beyer
Ferdinand Beyer

Reputation: 67157

But then, when I want to print it out via cout<<os; it prints out only the address (ie 0x6c180484). But I can't figure out, how to write it to be able to compile it and to get right values...

I guess you are passing a pointer of your object to std::cout, that gets printed as its address (hexadecimal number). If you have a pointer, make sure to de-reference it when passing it to a stream:

std::cout << *pointer;

Upvotes: 0

Related Questions