[sigc] A small question.



// Consider the program below. I have a question about it's behavior.

#include <sigc++/sigc++.h>
#include <iostream>
#include <string>

using namespace std;

class Physical : public sigc::trackable
{
public:
  Physical(int i) : id(i) {}
  virtual ~Physical() {}

  bool present(int i, Physical* phy)
  {
    if (id == i)
    {
      phy = this;

      cout << "Physical::present(): this: "
           << this << ", phy: " << phy << endl;

      phy->print("We put 'this' address in 'phy'."); // same as print().

      cout << "phy->id: " << phy->id << endl; // same as id.

      return true;
    }

    return false;
  }

  void print(const string& msg)
  { cout << msg << endl; }

private:
  int id;
};

int main()
{
  sigc::signal< bool, int, Physical* > signal_present;
  int id = 9;
  Physical p1(3), p2(id), p3(17), p4(22);
  Physical* phy = 0;

  bool answer = false;
  answer = signal_present.emit(4, phy); // Shows that not an instance,
                                        // does not affect us.

  cout << "answer: " << answer << ", phy: " << phy << endl;

  signal_present.connect(sigc::mem_fun(p1, &Physical::present));
  signal_present.connect(sigc::mem_fun(p2, &Physical::present));
  signal_present.connect(sigc::mem_fun(p3, &Physical::present));
  signal_present.connect(sigc::mem_fun(p4, &Physical::present));

  answer = signal_present.emit(7, phy); // Asking for an object of non existent
                                        // id, gives no response, as desired.
  answer = signal_present.emit(id, phy);// But this id exists.

  cout << "answer: " << answer << ", phy: " << phy << endl;

  if (answer)
  { cout << "An instance of id " << id << " is present.\n"; }

  if (phy)
  { phy->print("It works as assumed.\n"); }
}

/*
 * compilation:
 *     g++ sigc++_test.cc -o sigc++_test \ 
 *     `pkg-config sigc++-2.0 --cflags --libs`
 *
 * output:
 *     answer: 0, phy: 0
 *     Physical::present(): this: 0xbffff820, phy: 0xbffff820
 *     We put 'this' address in 'phy'.
 *     phy->id: 9
 *     answer: 0, phy: 0
 *
 * question:
 *     Why phy is 0 after signal emission? It was not null
 *     inside Physical::present(). Also answer is 0. What am I missing?
 *     What is the alternative to get address of phy similar to this method?
 *     I will be pleased for a detailed answer.
 */

//  -- Kashif Masood



		
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]