Results 1 to 4 of 4

Thread: Dynamic pointer type

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Dynamic pointer type

    Hi friends,

    I need to optimize a code but a few questions.
    I have a method that makes the checking of configurations that depending on the state set, and a pointer to the class X and if you're in another state, for class Y.

    These two classes are classes that have different but similar methods, I thought to use templates, but there is a template for pointer.
    What I want is a pointer to a type (class), sometimes to another (depending configuration), so I can access the appropriate attributes. How do I do that? It enables someone to write a minimum practical example? It's possible?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic pointer type

    If one class is a subclass of the other: access them both via the base pointer, use virtual methods for methods that are different in the base class.

    If the classes are only 'related' but not by inheritance, you may use templates. That only helps if the two classes may be used syntactically identically.
    Qt Code:
    1. class X
    2. {
    3. int x;
    4. int y;
    5. };
    6. class Y : public X
    7. {
    8. int z;
    9. };
    10.  
    11. bool check_config(const X * const config)
    12. {
    13. if (config->x > 5) return false;
    14. }
    15.  
    16. X x;
    17. Y y;
    18. check_config(&x);
    19. check_config(&y); // also ok
    To copy to clipboard, switch view to plain text mode 

    HTH
    If not, please give us more details (esp. on those configuration classes of your's).

  3. #3
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamic pointer type

    OK, follow little example for my question.
    In my particular case, the class does not inherit Z of X and Y.

    Qt Code:
    1. class X
    2. {
    3. public:
    4. int lastNumber( );
    5. };
    6.  
    7. class Y {
    8. public:
    9. int lastNumber( );
    10. };
    11.  
    12. class Z {
    13. private:
    14. bool check_config( );
    15. };
    16.  
    17. ...
    18.  
    19. bool Z::check_config( ) {
    20. if (result_of_database == 0)
    21. X *pointer = new X;
    22. else
    23. Y *pointer = new Y;
    24. ...
    25. return true;
    26. }
    27. ...
    28.  
    29. void Z::other_method( ) {
    30. pointer->lastNumber( ); // The correct pointer used depend result of check_config
    31. }
    To copy to clipboard, switch view to plain text mode 

    Please note that my code is wrong, but it is just for you understand, because the pointer was declared and initialized within the scope of the method.

    Thanks,

    Marcelo E. Geyer
    Brazil

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamic pointer type

    Basically, you have 3 options. You have to decide yourself, which is the appropriate in your situation:

    1) if classes X and Y are under your control: why not make them related by inheritance (perhaps a common base class)?

    2) provide two separate implementations for the two cases

    Qt Code:
    1. X * m_x; // init to NULL somewhere
    2. Y * m_y;
    3. bool Z::check_config( ) {
    4. if (result_of_database == 0)
    5. m_x = new X;
    6. else
    7. m_y = new Y;
    8. return true;
    9. }
    10. void Z::other_method( ) {
    11. if (m_x) m_x->lassNumber();
    12. if (m_y) m_y->lastNumber();
    13. pointer->lastNumber( );
    14. }
    To copy to clipboard, switch view to plain text mode 

    3) templates

    Note that that this is very similar to 2), just that the template saves you the work of having to write to separate other_methods, the compiler generates those for you.
    However, as soon as you want to do something for X that is not possible in Y, you have to code that in other_method. The template method is just a way to write code once for syntactically identically useable objects.

    Qt Code:
    1. void other_method()
    2. {
    3. if (m_x) do_other_method(m_x);
    4. if (m_y) do_other_method(m_y);
    5. }
    6.  
    7. template<typename Config>
    8. void Z::do_other_method(Config *cfg) {
    9. cfg->lassNumber();
    10. }
    To copy to clipboard, switch view to plain text mode 

    All code untried, but I hope the idea is understandable.
    HTH

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. Replies: 27
    Last Post: 16th May 2008, 07:33
  3. lib("dnsapi")
    By CHeader in forum Newbie
    Replies: 10
    Last Post: 16th February 2008, 19:21
  4. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  5. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38

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.