observer & virtual function

This is a discussion on observer & virtual function within the C++ Programming forums, part of the General Programming Boards category; Hi ppl! I need to write an observer, and this is what I've got so far: Code: #ifndef OB #define ...

  1. #1
    hsv
    hsv is offline
    Registered User
    Join Date
    May 2003
    Posts
    14

    Question observer & virtual function

    Hi ppl!

    I need to write an observer, and this is what I've got so far:

    Code:
    #ifndef OB
    #define OB
    #include <iostream>
    using namespace std;
    
    class observer
    {
    	public:
    	observer();
    	virtual void update(subject s);
    };
    
    class subject
    {
    	vector<observer> observers;
    	public:
    	void add_observer(observer& ob);
    	subject();
    	void notify();	
    };
    inline subject::subject()
    {
    }
    #endif
    The problem: void update() won't take a subject! Why is that? It works with add_observer so it should work with update, right?
    Does it show that i'm new to c++?

    /Henrik

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    There is a library (source code available) called Loki, that implements an observer. There is even a book that explains the source code more or less line by line. To be honest, I don't remember the exact details of the implementation well enough to help you, but it may be helpful if you downloaded Loki and looked at the observer source code. There's other neat stuff like smart pointers as well.

    Just search for C++ and Loki on google and it should come up right away. Good luck.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    hsv
    hsv is offline
    Registered User
    Join Date
    May 2003
    Posts
    14
    Ok, I'll check it out.. thanks!

  4. #4
    hsv
    hsv is offline
    Registered User
    Join Date
    May 2003
    Posts
    14
    I couldn't find any observer there, but I changed update() to take an int (id of the subject).

    Anyway.. a new problem!

    Code:
    void subject::notify()
    {
    	
    	for(vector<observer>::iterator p = observers.begin(); p != observers.end(); p++)
    	{
    		p->update(subject_id); 
                    }
    }
    What happens is that the update() in observer is called, not in it's sub-class. Why?

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Because the vector stores actualy observer object. If you are to use polymorphism you must store pointers.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    hsv
    hsv is offline
    Registered User
    Join Date
    May 2003
    Posts
    14
    Thanks! works perfectly now..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 10:25 AM
  2. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 03:02 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21