   A little OOP confirmation!!
  Hi there i was just reading up on some C++ class stuff and came upon a question. I woud like alittle clarification on as to wether or not i am understanding this correctly.The example below will be used to illustrate a class Base and a derived class and then ill explain more on what i need to know / just want to make sure i got it right.class BASE{public:   virtual void Draw();   {std::cout <<"This is a Test from the base";}};class DERIVED{public:   virtual void Draw();   {std::cout <<"This is a Test from the Derived";}};int main(){DERIVED Test;BASE Testy;Testy = Test;Testy.Draw();return 0;}Now what i am really intrested in is the slice process. When i take the derived class and make it equal a base class from my understanding the functions from the Derivied class replace the Base class.As i have been reading this is because the Virtual functions are kept in a virtual table of pointers and the Derivied's function pointer replaces the base's pointer.If that is so, can i still acces the information from the derivied's class. For instance i am trying to do my own GUI and i will be useing Direct Draw as a rendering context. Can the Base class call the Derivied class's Draw function and does the function still have access to the Image data or does all that need to be stored in the Base class as well.The however wouldn't be very helpful or useful. Just abit of clerafication on that would nice.On a side note For an edit control what would be a good way of keeping track and storing text that is to be drawin into the control regieon and implmentation of a scroll bar.Thanks for any input on any of the above.Regards Jouei.   
  ==edit==ive just reread you comment again, sorry, the answer is no a base class cant call its derived class methods.btw to explicily say you are inheriting the class you need to do ssomething like thisclass DERIVED : public BASE{// code} [Edited by - terry_burns85 on September 8, 2008 10:52:14 PM];Quote:Original post by JoueiThe example below will be used to illustrate a class Base and a derived class and then ill explain more on what i need to know / just want to make sure i got it right.Your example uses the names 'base' and 'derived', but neither is actually a base or derived class.Quote:Now what i am really intrested in is the slice process. When i take the derived class and make it equal a base class from my understanding the functions from the Derivied class replace the Base class.No, that doesn't even compile in C++ at the moment because of the above reason.ClassA Test;ClassB Testy;Testy = Test;//Error cannot convert ClassA to ClassBI assume you actually meant to use inheritance though, so If we re-write your example then this is the results:class BASE{public:   void BaseFunc() { Draw(); }   int variable;   virtual void Draw();   {std::cout <<"This is a Test from the base";}};class DERIVED : public BASE{public:   virtual void Draw();   {std::cout <<"This is a Test from the Derived";}};int main(){DERIVED derived;BASE base;derived.variable = 42;base.variable = 0;derived.Draw();//"This is a Test from the Derived"derived.BaseFunc();//"This is a Test from the Derived"cout << derived.variable;// 42base.Draw();//"This is a Test from the base"base.BaseFunc();//"This is a Test from the base"cout << base.variable;// 0base = derived;//slice off derived members, only copy base membersbase.Draw();//"This is a Test from the base"base.BaseFunc();//"This is a Test from the base"cout << base.variable;// 42BASE& reference = derived;// <-- polymorphism!reference.Draw();//"This is a Test from the Derived"reference.BaseFunc();//"This is a Test from the Derived"cout << reference.variable;// 42}When you perform the assignment (//slice), all of the 'derived part' of Test gets cut off - only the 'base part' gets copied over to Testy. As you can see by the comments, the only thing that gets copied is the member variable (0 becomes 42).   ;  A virtual function called from within a base class function does reach the function implementation of that derived class.  That is the one way in which a derived class function can be called from within a base class function.  But the actual type must be of the derived class of course.I'm not even sure you're implying the contrary, and that my clarification helps any (having trouble understanding the true points here :) ) but I just wanted to make that clarification.If your goal is to copy objects abstractly, you may want to consider dealing with dynamic allocation and pointers, and maybe create a virtual cloning function that creates a duplicate instance of matching type, but the pointer returned is the base type.   ;  Yeah Sorry i forgot to public the base class X.X my bad but the question remains the same.Lets try that again.so if i have two class's one a baseclass BASE{public:  virtual void Draw();};class DERIVIED : public BASE{public:  virtual void Draw()  {  //Do drawing with DDSurface  }private:  IDirectDrawSurface8 * DDSurface;};My real question is if if slice the Derivied class into the BASE class and call Draw from what iv heard and read is that it will call the Draw from my Derivied class. If thats true would there be any problems with the Drawing surface access.Basicaly this is the Idea i have a std::list for all my gui but only the active ones. Each controls type for my GUI is a derived class from a Base the std::list is of the base class type.Would that work properly or is the more issues here then i am reading from my book. Because as far as i understand it it should work something liket hat.Again thanks for any input and sorry for the earlyer blunder.Regards Jouei.   ;Quote:Original post by JoueiMy real question is if if slice the Derivied class into the BASE class...bad things will happen. Among other details, the DDSurface data member (which does not exist in the base class) will disappearm and the type of the sliced object will remain BASE. See, in C++, you can't change the type of a value at runtime, and assignment merely changes the content of a value, meaning that if you assign anything to a BASE, what you are left with is a BASE.In order for polymorphism to work, you need to work with your values by reference (either using references or, if you need reseating, pointers).   ;Quote:Original post by Joueii have a std::list for all my gui but only the active ones. Each controls type for my GUI is a derived class from a Base the std::list is of the base class type.Quote:Original post by ToohrVykIn order for polymorphism to work, you need to work with your values by reference (either using references or, if you need reseating, pointers).And in this specific case, we have to use pointers since you can't store references in a std::list (because references aren't values).std::list<Base*> list; // or better yet:std::list<boost::shared_ptr<Base>> list;