bool changes within Loki::StaticDispatcher

This is a discussion on bool changes within Loki::StaticDispatcher within the C++ Programming forums, part of the General Programming Boards category; Hello! I'm new to this forum and i'm not english. Hope you can help me either. I can't understand, why ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    1

    bool changes within Loki::StaticDispatcher

    Hello!

    I'm new to this forum and i'm not english. Hope you can help me either.

    I can't understand, why the bool changes, although i'm not setting him to false anywhere:

    Code:
    #include <iostream>
    #include <loki/MultiMethods.h>
    
    struct Base
    {
        virtual ~Base() {}
    };
    
    struct Widget : public Base
    {
        Widget(int x = 0) : x_(x) {}
        int x_;
    };
    
    class HatchingExecutor
    {
    public:
        void Fire(Widget& lhs, Widget& rhs)
        {
            if(lhs.x_ == rhs.x_)
            {
                bool_ = true;
            }
            std::cout << "Bool: " << bool_ << " Adresse: " << &bool_ << std::endl;
        }
        void OnError(Base&, Base&) {}
        HatchingExecutor() : bool_(false) { std::cout << "Konstruktor" << std::endl; }
    private:
        bool bool_;
    };
    
    int main()
    {
        HatchingExecutor exec;
        typedef Loki::StaticDispatcher<HatchingExecutor, Base, LOKI_TYPELIST_1(Widget)> Dispatch;
    
        Widget a;
        Widget b(1);
        Widget c;
    
        Dispatch::Go(a, c, exec);
        Dispatch::Go(a, b, exec);
        Dispatch::Go(c, b, exec);
    }
    It shows:
    Code:
    Konstruktor
    Bool: 1 Adresse: 0xbfae3368
    Bool: 0 Adresse: 0xbfae3368
    Bool: 0 Adresse: 0xbfae3368
    I've suggested it would be one everytime, but where is it changed to false?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    I've never heard of this Loki library and I'm sure it's not just me. If you're going to use non-standard libraries, it's best to post some code from that library, as well. Like, for instance, the Go() function and any function used in it.
    Sent from my iPad®

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If you're passing a copy of your class rather than a reference or a pointer, it would cause a new instance of it to be constructed. The if
    Code:
            if(lhs.x_ == rhs.x_)
            {
                bool_ = true;
            }
    might prove false, and bool_ would get the default boolean value, which is false. [edit] Or rather it would retain the value it was assigned in the constructor, as indicated two posts down. [/edit]

    You could detect this by putting an else after the if:
    Code:
    else cout << "bool_ is now false\n";
    You could fix this by removing the if or passing references/pointers.

    [edit]
    I've never heard of this Loki library
    Where are your googling skills?
    http://sourceforge.net/projects/loki-lib/ [/edit]
    Last edited by dwks; 08-05-2006 at 02:30 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Quote Originally Posted by dwks
    [edit]

    Where are your googling skills?
    http://sourceforge.net/projects/loki-lib/ [/edit]
    I'm not going to go hunting down code for some random person who chooses not to provide enough of it. I'm sure he would be willing to post the missing functions. However, if what I need to see to help isn't either in my knowledge bank or right in front of my eyes, I'm not going to put to much effort into an answer.

    That's just me, though. It was kind of you to go searching for him.
    Sent from my iPad®

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,532
    The bool is being set to false on the class constructor.

    Code:
    HatchingExecutor() : bool_(false) { std::cout << "Konstruktor" << std::endl; }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Code behaves as by design. I don't know why you'd think it would be 1 every time.

    For what it's worth, Loki is a library written by Andrei Alexandrescu as a general toolset and a companion for his book Modern C++ Design, which I highly recommend reading.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 05:25 AM
  5. Need Help With an RPG Style Game
    By JayDog in forum Game Programming
    Replies: 6
    Last Post: 03-30-2003, 08:43 PM

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