Re: [sigc] A small question.



On Fri, 2005-04-29 at 09:05 -0700, Kashif Masood wrote:
> // 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
  This is correct result after first signal

>  *     Physical::present(): this: 0xbffff820, phy: 0xbffff820
>  *     We put 'this' address in 'phy'.
>  *     phy->id: 9
  This is correct answer of second connected object

>  *     answer: 0, phy: 0
  This is correct return of result of last connected object invocation.
You have not installed marshallers, so in the event of multiple
connections, it returns the last connected slot results.

Further, you have passed a pointer, not a ptr-to-pointer, so stuffing
phy with 'this' ptr only has local effect, it does not return new phy
value to calling function (this bit is elementary C).

>  *
>  * 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?
  Pass Physical** ptr and bool* would be simplest.  Otherwise register
Physical objects with some other data structure or function.  Otherwise,
marshall the return values, but then you have to scan through an array
looking for the result, which kind of defeats the purpose of the
function.

>  *     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/ 
> _______________________________________________
> libsigc-list mailing list
> libsigc-list gnome org
> http://mail.gnome.org/mailman/listinfo/libsigc-list
-- 
Carl Nygard <cjnygard verizon net>




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