Jump to content
СофтФорум - всё о компьютерах и не только

virtual и operator<< в c++


Recommended Posts

всем привет, как сделать так, чтобы перегруженный operator<< выполнялся для объекта нужного типа в функциях Stack::peek() и Stack::pop(), дружественные функции не могут быть виртуальными, а если перегрузить operator<<, как не дружественную функцию и объявить ее виртуальной, то решение получается не очень красивым, кто знает подскажите, пожалуйста, другой способ если, кончено, он есть

class Shape {   public:       Shape() cout << "Shape constructor\n";}       virtual ~Shape() {cout << "Shape destructor\n";}       virtual void draw() {cout << "Shape::draw\n";}       friend ostream& operator<<(ostream& os, const Shape& c) {           return os << "Shape::operator<<\n";       }};class Circle : public Shape {   public:       Circle() {cout << "Circle constructor\n";}       ~Circle() {cout << "Circle destructor\n";}       void draw() {cout << "Circle::draw\n";}       friend ostream& operator<<(ostream& os, const Circle&) {           return os << "Circle::operator<<\n";       }};class Square : public Shape {   public:       Square() {cout << "Square constructor\n";}       ~Square() {cout << "Square destructor\n";}       void draw() {cout << "Square::draw\n";}       friend ostream& operator<<(ostream& os, const Square&) {           return os << "Square::operator<<\n";       }};class Triangle : public Shape {   public:       Triangle() {cout << "Triangle constructor\n";}       ~Triangle() {cout << "Triangle destructor\n";}       void draw() {cout << "Triangle::draw\n";}       friend ostream& operator<<(ostream& os, const Triangle&) {           return os << "Triangle::operator<<\n";       }};class Stack {   private:       struct Link {           Shape* data;           Link* next;           Link(Shape* dat, Link* nxt) : data(dat), next(nxt) {}       }* head;       int sz;   public:       Stack() : head(0), sz(0) {}       ~Stack();       void push(Shape* dat) {           head = new Link(dat, head);           sz++;       }       Shape* peek() const {           return head ? head->data : 0;       }       Shape* pop();       int size() {return sz;}};Stack::~Stack() {   while (head)      pop();}Shape* Stack::pop() {   if (head == 0) return 0;   Shape* result = head->data;   Link* oldHead = head;   head = head->next;   delete oldHead;   sz--;   return result;}
Link to comment
Share on other sites

  • 2 weeks later...
The major disadvantage of friend functions is that they require an extra line of code when you want dynamic binding. To get the effect of a virtual friend, the friend function should call a hidden (usually protected) virtual member function. This is called the Virtual Friend Function Idiom.

Если не понимаете английского, здесь предлагають чтоби operator << делал

c.cout (примерно)

class Shape {   public:       Shape() cout << "Shape constructor\n";}       virtual ~Shape() {cout << "Shape destructor\n";}       virtual void draw() {cout << "Shape::draw\n";}       friend ostream& operator<<(ostream& os, const Shape& c) {           return c.cout(os);       }   protected:        virttual ostream& count(ostream& os){           return os << "Shape::operator<<\n";        }};
Link to comment
Share on other sites

  • 1 month later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...