Results 1 to 12 of 12

Thread: inheritance

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default inheritance

    hi, I 've got a simple problem with subclass and method (I'll leave out some keywords to go direct to problem...).This isn't real C++ but the concept shouldn't change...
    Qt Code:
    1. abstract baseClass {
    2. List <baseClass> list = new List<BaseClass>();
    3. List<Two> list_two=new List<Two>();
    4.  
    5. virtual add(BaseClass class) {
    6. list.Add(class);
    7. if (class.Type is "Two" also do.......)
    8. list_two.Add(class); //here error, because class hasn't same type of list_two...
    9. }
    10. }
    11.  
    12.  
    13. class One : baseClass {
    14. override void add(BaseClass class) {
    15. // do something
    16. base.add(class);
    17. }
    18. }
    19.  
    20.  
    21. class Two : baseClass{
    22. //constructor and the rest
    23. }
    24.  
    25. main {
    26. Two two;
    27. One one;
    28. one.add(new two() );
    29. }
    To copy to clipboard, switch view to plain text mode 
    Must I have refactor the classes or how can I do this things?
    thanks...
    Regards

  2. #2
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: inheritance

    If you've already established that the type of the class is Two, you can just do a static cast.

    list_two.Add(static_cast<Two>(class));
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: inheritance

    What happens if you make the list store pointers?

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: inheritance

    I was assuming that the list was storing pointers anyway. A static cast will work. But, like I said, only if you're already sure 'class' is pointing to a 'Two' instance.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: inheritance

    HI,
    1. and If list_two contain pointer????
    2. I'm wondering if the control of the type class and the list_two.add was better put it inside the override member instead of virtual..
    Base class can point to derived class; but if (and will be so) Two has some field more than base class, what happen to this fields in derived -> base convertion?
    if I have:
    Qt Code:
    1. void doSOmething(BaseClass base) {
    2. base.field1; // ok field1 was in the base
    3. base.field2 //base hasn't field2: it can't used it here.
    4. base.fieldN;
    5. }
    6. //main
    7. Two two;
    8. doSomething(two);
    To copy to clipboard, switch view to plain text mode 
    Is in this way that work the things, please?
    thanks.
    Last edited by mickey; 28th September 2007 at 16:56.
    Regards

  6. #6
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: inheritance

    I'm sorry, but your English is a bit unclear (and one question mark is really enough).

    Would you rephrase the question, and give us an example in actual C++?
    Last edited by Michiel; 28th September 2007 at 19:37. Reason: spelling error
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: inheritance

    Hi, but this isn't c++......However:
    Qt Code:
    1. abstract baseClass {
    2. List <baseClass> list = new List<BaseClass>();
    3. List<Two> list_two=new List<Two>();
    4.  
    5. virtual add(BaseClass class) {
    6. list.Add(class);
    7.  
    8. if (class.Type is "Two" ) list_two.Add((Two) class);
    9. /* is correct put this above here? Maybe Was better put it in [1] ? */
    10. class.value = 10; //does it make sense???
    11. }
    12. }
    13.  
    14. class One : baseClass {
    15. One (){}
    16. override void add(BaseClass class) {
    17. // ----------- [1]
    18. // do something
    19. base.add(class);
    20. }
    21. }
    22.  
    23.  
    24. class Two : baseClass{
    25. Two() {}
    26. //constructor and the rest
    27. int value;
    28.  
    29. }
    30.  
    31. main {
    32. Two two;
    33. One one;
    34. one.add(new two() ); // here the object two is "converted" to its base class; the base
    35. //class hasn't value field: is this "convertion" and the "cast" like you have advice,
    36. //could have any problem ???
    37. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  8. #8
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: inheritance

    If your problem is only with the conversion of Base* to Two* when the argument is genuinely Two* type, then its absolutely safe to use static cast to convert Base* to Two*.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: inheritance

    Well what i said above is not true in all cases. I'll explain it through code

    Qt Code:
    1. #include <iostream>
    2.  
    3. using namespace std;
    4. class Derived;
    5. class Base
    6. {
    7. public:
    8. virtual ~Base() {}
    9.  
    10. virtual void add(Base *b)
    11. {
    12. //assume i somehow determine b is actually Derived* type
    13. Derived *d = static_cast<Derived*)(b);
    14. // But still this doesn't compile at all!!
    15. }
    16. void anotherFunc(Base *b);
    17. };
    18.  
    19. class Derived : public Base
    20. {
    21. public:
    22. Derived() { add(this); }
    23.  
    24. void sayHello()
    25. {
    26. cout << "Hello" << endl;
    27. }
    28. };
    29.  
    30. void Base::anotherFunc(Base *b)
    31. {
    32. //assume i somehow determine b is actually Derived* type
    33. Derived *d = static_cast<Derived*)(b);
    34. //This succeeds!!!!
    35. }
    36. int main()
    37. {
    38. Derived *d;
    39. Base *b = new Base;
    40. d = static_cast<Derived*>(b);
    41. // This too succeeds
    42. int c;
    43. cin >> c;
    44. return 0;
    45. }
    To copy to clipboard, switch view to plain text mode 

    So basically for the cast to work the compiler should have the definition of class type being casted to at the place of casting. That is to say if the compiler doesn't choke on cast and if you somehow determine that given class is Derived type you can use static_cast
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: inheritance

    I don't think we'll solve the issue with pseudo code. Logically according to inheritance paradigm the code is correct and should work without any casts. So if something doesn't work, I suggest you show us the actual code.

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: inheritance

    It'sn't c++; it's C# (apart the main); But it works now; what I was wondering is if it's a good use of inheritance or other.....
    thanks
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: inheritance

    Yes, it's a proper use of inheritance.

Similar Threads

  1. Object and multiple inheritance for interfaces
    By brcain in forum Qt Programming
    Replies: 8
    Last Post: 29th June 2021, 15:29
  2. How much inheritance do you use?
    By Michiel in forum General Programming
    Replies: 8
    Last Post: 1st August 2006, 22:29
  3. Inheritance and QpaintEvent
    By djoul in forum Qt Programming
    Replies: 22
    Last Post: 5th July 2006, 13:56
  4. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  5. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.