Thread: looking for better way to do this possibly

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    looking for better way to do this possibly

    In the following code:
    Code:
    #include <iostream>
    
    class Base{};
    
    class Bar{
        public:
            Bar() : fptr(0) {}
            const Base* get_origin() const { return fptr; }
        private:
            Base* fptr;
        friend class Foo;
    };
    
    class Foo : public Base{
        public:
            Bar make_bar(){
                Bar b;
                b.fptr = this;
                return b;
            }
    };
    
    void test_equality( const Base* f1, const Base* f2 ){
        if( f1 == f2 )
            std::cout << "Pointers are equal.\n";
        else
            std::cout << "Pointers are not equal.\n";
    }
    
    int main(){
    
        Foo f;
        Bar b  = f.make_bar();
        Bar b2 = f.make_bar();
        
        Bar c;
    
        test_equality( b.get_origin(), b2.get_origin() );
        test_equality( b.get_origin(),  c.get_origin() );
        
        return 0;
    }
    I want to make sure that a Bar instantiated on its own is never "equal" to a Bar created by a Foo by checking the Base pointer. I had to add the Base class and derive Foo from it because declaring a Foo pointer in Bar results in error( declaration of Foo not found ).

    Is there an easier/more-elegant way to achieve this kind of checking paradigm?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    How about:
    Code:
    class Bar
    {
      bool createdByFoo;
      friend class Foo;
      Bar(bool) : createdByFoo(true) {}
    public:
      Bar() : createdByFoo(false) {}
    };
    
    class Foo
    {
    public:
      Bar make_bar() { return Bar(true); }
    };
    Then define the comparison appropriately.
    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

  3. #3
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Thanks a lot. That definitely solves one of my earlier issues, however i really want a pointer to a Foo because I don't want Bar created by different Foos to be equal either -I apologize, I realize I didn't specify that in my OP.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Then use a forward declaration instead of the ugly base class.
    Code:
    class Foo;
    class Bar
    {
      Foo *theFoo;
      friend class Foo;
      Bar(Foo *foo) : theFoo(foo) {}
    public:
      Bar() : theFoo(0) {}
    };
    
    class Foo
    {
    public:
      Bar make_bar() { return Bar(this); }
    };
    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

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Worked like a charm! I didn't go with the forward declaration because it failed in another (similar) problem I was having, so I assumed it wouldn't work for this one.

    Mucho thanks for the private constructor tip, so simple, yet I never would've thought to do it that way.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2007, 02:06 AM
  2. What the heck am I possibly doing wrong?
    By QuestionC in forum Tech Board
    Replies: 2
    Last Post: 04-29-2007, 08:43 PM
  3. A-Team to possibly become a movie
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-12-2004, 09:07 PM
  4. advice and possibly guidance
    By nazri81 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 10:19 PM
  5. What could have possibly caused this garbage?
    By kreyes in forum C Programming
    Replies: 4
    Last Post: 03-07-2002, 08:24 PM