Results 1 to 20 of 24

Thread: Defining shared class member functions only once

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    43
    Thanked 4 Times in 2 Posts

    Default Re: Defining shared class member functions only once

    Thanks for your response!

    Quote Originally Posted by amleto View Post
    why cant
    quint16 rgb[3]
    be protected and in CORE?
    Normally I would just make a class that encapsulates every possible combination and be done with it, drastically simplifying my code. The problem rests in the problem of scale—when you have an extra 60 bytes (more or less) per record floating around, and around 6,000,000 records, that's an extra 343MB that I need to drag around. I'm already pushing the available memory as it is, so my intent is to only include RGB/TIME/ALPHA when the records actually have that data.

    Quote Originally Posted by amleto View Post
    If you put members variables into CORE, then you need to inherit CORE virtually, to avoid the dreaded diamond.
    Can you please elaborate on this? I'm not a programmer by training, so I'm unfamiliar with some of the lingo. Also, what do you mean by inheriting it virtually? I've never been clear on the functional distinction between Protected and Private. If I make the prototyped member functions of CORE protected, then my generalized manipulation functions (using CORE* pointers, though the object pointed to will be A, B, C, or D depending on the data imported) throw a compiler error.

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Defining shared class member functions only once

    Hi,

    You could create a 'class' or 'type' RGB which has public members r, g and b. It would have a method setRGB() to set these three members, and also the other member functions you mention.

    The A and C classes would have a member of type RGB, say m_rgb. If a method in this class would need to know anything about the color, it would use the members m_rgb.r, m_rgb.g and m_rgb.b.

    This way you don't have to implement anything in your base classes, or in any other class in which you would like to use a color.

    Best regards,
    Marc

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by Phlucious View Post
    Thanks for your response!


    Normally I would just make a class that encapsulates every possible combination and be done with it, drastically simplifying my code. The problem rests in the problem of scale—when you have an extra 60 bytes (more or less) per record floating around, and around 6,000,000 records, that's an extra 343MB that I need to drag around. I'm already pushing the available memory as it is, so my intent is to only include RGB/TIME/ALPHA when the records actually have that data.


    Can you please elaborate on this? I'm not a programmer by training, so I'm unfamiliar with some of the lingo. Also, what do you mean by inheriting it virtually? I've never been clear on the functional distinction between Protected and Private. If I make the prototyped member functions of CORE protected, then my generalized manipulation functions (using CORE* pointers, though the object pointed to will be A, B, C, or D depending on the data imported) throw a compiler error.
    inheriting virtualy is not to do with access level (public/private/protected).

    If you have some base class B, and class A and class C both inherit B, then some other class, D is designed that wants to inherit A and C we have a problem IF B has data members.

    Qt Code:
    1. B
    2. /\
    3. A C
    4. \/
    5. D
    To copy to clipboard, switch view to plain text mode 

    If B has data members, then they exist as part of A, and ALSO as part of C. This is a big problem. To get around this, inherit B virtually (use virtual keyword like virtual method). Then if some class has multiple instances of B, only one will actually be made.


    However, since you say you can't use the members in the base class, you wont have this problem.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. The following user says thank you to amleto for this useful post:

    Phlucious (27th December 2011)

  5. #4
    Join Date
    Jan 2011
    Posts
    70
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    43
    Thanked 4 Times in 2 Posts

    Default Re: Defining shared class member functions only once

    Thank you for the great suggestion! Inheriting virtually seems to have the intended results. Following your diamond structure...

    Qt Code:
    1. class B {}
    2. class A : virtual public B {}
    3. class C : virtual public B {}
    4. class D : public A, public C {}
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, I get the C4250 "[derived class] inherits [overwritten method in derived class] via dominance" warning now for every virtual function I re-defined. I understand that this warning is for programmers that might be caught by surprise, but is there any syntactic way for me to indicate to MSVC2010 that I'm doing this on purpose?

    [edit]
    I found this link useful for understanding what you wrote:
    http://publib.boulder.ibm.com/infoce...%2Fcplr135.htm
    [/edit]

  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    3
    Thanked 127 Times in 126 Posts

    Default Re: Defining shared class member functions only once

    this warning is a bit different - as long as you have read and undersand what it warns against, and that if you have two reimplemented virtuals at the same inheritance level, then that would be an error.

    you can supress warnings with #pragma or project settings

    http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. The following user says thank you to amleto for this useful post:

    Phlucious (29th December 2011)

  8. #6
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Defining shared class member functions only once

    Still, I think multiple inheritance is not the right solution for your initial problem.

    Say you have a class 'bike', and if you have a class 'livingroom'. Both have a light that you can switch on and off. That doesn't mean it is a good idea to have a class 'light' and derive 'bike' and 'livingroom' from it.

    Given your level of expertise in C++, it might be better to keep things simple. Plain old library functions are still widely used. It is not because C++ supports object oriented design, that everything has to be implemented in an object-oriented fashion. And static 'utility' functions are also widely used for a class where the function doesn't work on an object itself, but sort of belongs to the class anyway.

    Regards,
    Marc

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

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by marcvanriet View Post
    Say you have a class 'bike', and if you have a class 'livingroom'. Both have a light that you can switch on and off. That doesn't mean it is a good idea to have a class 'light' and derive 'bike' and 'livingroom' from it.
    What if you call the class 'Lightable' and not 'light'?
    Qt Code:
    1. class Lightable {
    2. public:
    3. virtual void enableLight() { ... }
    4. virtual void disableLight() { ... }
    5. };
    6.  
    7. class Bike : public Vehice, public Lightable { ... };
    8. class LivingRoom : public Chamber, public Lightable { ... };
    To copy to clipboard, switch view to plain text mode 
    Sounds reasonable to me...


    An alternative is to have an external helper class that implements solely the "shared" functionality and call that object's methods passing the object it is to manipulate. Then you need implementations of that class to handle objects of specific types (ones that have the "RGB" functionality and those of the "TIME" functionality). Patterns like 'visitor' or 'strategy' come to my mind here but probably more solutions are possible and viable.
    In the end you have one "instance" of functionality per class instead of having one per item which escapes the memory hog trap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by wysota View Post
    What if you call the class 'Lightable' and not 'light'?
    Sounds reasonable to me...
    Sure, but all depends on the situation. What if you then come across a bike of which the front light can be set to three states : on - blinking - off. And when there are multiple independently controlled lights in the chamber ? Unfortunately the OP didn't post enough details of the use case to say if each object can really have only 1 color. Maybe later on there could be objects that have a 'front' and 'back' color.

    I would probably prefer an interface like
    bike1->frontlight->enableLight();
    room1->ceilinglight->disableLight();
    room1->curtainlights->enableLight();
    If the 'frontlight' and so have a reference to the class of which they are member, events can be triggered when a light status changes if this is necessary.

    But again... all depends on the use case.

    Regards,
    Marc

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

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by marcvanriet View Post
    Sure, but all depends on the situation. What if you then come across a bike of which the front light can be set to three states : on - blinking - off. And when there are multiple independently controlled lights in the chamber ?
    Then you extend the Lightable interface. What you say applies to any class, inheritance, single or multiple, is irrelevant here. If you continue this way, you'll reach a conclusion that there is no point in creating classes at all because some day you might want to change something in your program breaking what you did before.



    I would probably prefer an interface like
    bike1->frontlight->enableLight();
    room1->ceilinglight->disableLight();
    room1->curtainlights->enableLight();
    If the 'frontlight' and so have a reference to the class of which they are member, events can be triggered when a light status changes if this is necessary.
    This changes nothing really. You still have to be able to know that some object has a "frontlight", "ceilinglight" and "curtainlight" members and that some other has none of them. The point is to have a common base class and then say "if this object has a ceiling light, turn it on". For that you can use double dispatch.

    Or in terms of C++:
    Qt Code:
    1. class Visitor {
    2. public:
    3. virtual void visitLivingRoom(LivingRoom *) {};
    4. virtual void visitBike(Bike *) {};
    5. };
    6.  
    7. class Item {
    8. public:
    9. virtual void accept(Visitor &visitor) = 0;
    10. };
    11.  
    12. class LivingRoom : public Item {
    13. public:
    14. void accept(Visitor &visitor) { visitor.visitLivingRoom(this); }
    15. };
    16.  
    17. class Bike : public Item {
    18. public:
    19. void accept(Visitor &visitor) { visitor.visitBike(this); }
    20. };
    To copy to clipboard, switch view to plain text mode 

    And then:

    Qt Code:
    1. class TurnLightsOnBikes : public Visitor {
    2. public:
    3. void visitBike(Bike *bike) { bike->turnLightsOn(); }
    4. };
    5.  
    6.  
    7. QList<Item*> items = ...
    8. TurnLightsOnBikes bVisitor;
    9. foreach(Item *item, items) {
    10. item->accept(bVisitor);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Since C++ doesn't have double dispatch (only single dispatch through virtual methods) it has to be emulated like in the code above. Furthermore the visitor can be made a friend of Item subclasses to allow access to private members.

    In the "adding more lights" case it is enough to modify the visitor or create a new one. When a new subclass of Item is added, it is enough to add a new method to the Visitor interface. In all cases the Item class API remains clean and functionality is added by implementing a new Visitor subclass (which gives a nice separation and extendability).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #10
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Defining shared class member functions only once

    Phlucious: I'm not a programmer by training, so I'm unfamiliar with some of the lingo.
    Wysota, I think we've lost someone in the discussion here..

    Maybe my example with the bike and chamber didn't really fit the OP's original problem. That's the danger of an OP asking a generic question without a real-life example. And that's also the danger for a replier of using a counter-example that is taken in other directions then intended.

    Anyway, what I mean to say is that OOP techniques aren't necessarely ALWAYS the best techniques for handling a problem. Especially for a novice. Can we agree on that ?

    Thanks for the pointer to the 'visitor' pattern. Didn't know that, will study it in more detail.

    Regards,
    Marc

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

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by marcvanriet View Post
    Anyway, what I mean to say is that OOP techniques aren't necessarely ALWAYS the best techniques for handling a problem. Especially for a novice. Can we agree on that ?
    I think multiple inheritance is a viable approach here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #12
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    10
    Thanked 31 Times in 25 Posts

    Default Re: Defining shared class member functions only once

    OK, you win

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

    Default Re: Defining shared class member functions only once

    You have the right to have your own opinion, as do I to have mine. Nobody wins or loses here.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #14
    Join Date
    Jan 2011
    Posts
    70
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    43
    Thanked 4 Times in 2 Posts

    Default Re: Defining shared class member functions only once

    Quote Originally Posted by wysota View Post
    An alternative is to have an external helper class that implements solely the "shared" functionality and call that object's methods passing the object it is to manipulate. Then you need implementations of that class to handle objects of specific types (ones that have the "RGB" functionality and those of the "TIME" functionality). Patterns like 'visitor' or 'strategy' come to my mind here but probably more solutions are possible and viable.

    In the end you have one "instance" of functionality per class instead of having one per item which escapes the memory hog trap.
    I'm intrigued by this possibility. Are you basically describing the situation of having specialized storage classes with one generalized access class?

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

    Default Re: Defining shared class member functions only once

    More like having specialized functionality classes with one generalized structure/access class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 5
    Last Post: 26th September 2011, 09:12
  2. Pointer to non static member functions within the class
    By Lykurg in forum General Programming
    Replies: 3
    Last Post: 24th April 2011, 07:37
  3. how to document classes and member functions?
    By jackal in forum Qt Programming
    Replies: 15
    Last Post: 9th April 2011, 22:02
  4. Replies: 1
    Last Post: 16th March 2011, 08:10
  5. Pointers to Member Functions
    By Doug Broadwell in forum General Programming
    Replies: 8
    Last Post: 15th May 2007, 23:08

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
  •  
Qt is a trademark of The Qt Company.