Thread: Source type not polymorphic

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

    Source type not polymorphic

    I want to be able to add the functionality (somehow) in red. Any ideas?
    Code:
    #include <iostream>
     
    struct Foo{
     
      public:
        int getInt(){ return mInt; }
     
      protected:
        int mInt;
     
      friend class FooModifier;
    };
     
    class Bar : public Foo{
     
      public:
        char getChar(){ return mChar; }
     
      private:
        char mChar;
     
      friend class FooModifier;
    };
     
    class FooModifier{
     
      public:
        FooModifier( Foo* f )
        : mFoo( f ){}
     
        void setInt( int n ){ mFoo->mInt = n; }
        void setChar( char c ){ dynamic_cast<Bar*>(mFoo)->mChar = c; } // Source of problem
     
      private:
        Foo* mFoo;
    };
     
     
    int main(){
     
      Foo* f = new Foo();
      FooModifier fm(f);
      fm.setInt(5);
      std::cout << f->getInt() << "\n";
     
      Bar* b = new Bar();
      FooModifier fm2( dynamic_cast<Foo*>(b) );
      fm2.setInt(7);
      std::cout << b->getInt() << "\n";
     
      // Want to be able to modify both foo and bars
      fm2.setChar('k');
     std::cout << b->getChar() << "\n";
     
      return 0;
    }
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can add a virtual getter/setter.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    assuming you haven't dumbed down the types you're using for the purposes of your example, you could use a polymorphic setter method. you can assign int to char just fine, it will just ignore the upper 3 bytes.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This design doesn't seem right to me. A Bar does not work like a Foo because a Bar has a char member that can be retrieved and set.

    It would make more sense if Foo had virtual functions that could be called by FooModifier to handle all necessary tasks.
    '
    It seems as if Bar should really contain Foo, or they should be unrelated.

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Nevermind, I don't need to be able to modify bar's extra stuff that foo doesn't havve. Sorry for the bother.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 12:22 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM