Results 1 to 14 of 14

Thread: How can I: Know the class name of the caller...

  1. #1
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question How can I: Know the class name of the caller...

    Hi,

    How can I know the name of the parent class...
    For instance I've a class called "class_a" and I've to classes "class1" and "class2" calling it, but I need to change the output a little bit to fit the situation.

    I've tired *parent but I'm not really understanding how to do it.

    Thanks.
    Last edited by TCB13; 8th September 2011 at 00:10.

  2. #2
    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: How can I: Know the parent class name...

    Is it a Qt question or a C++ question? And by "parent class" do you mean a "super class" or a "parent object 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.


  3. #3
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I: Know the parent class name...

    Quote Originally Posted by wysota View Post
    Is it a Qt question or a C++ question? And by "parent class" do you mean a "super class" or a "parent object class"?
    Well... both... I'm new to this QT/Classes stuff

    Anyway I need to know in the "class_a" code the name of the class who's calling it...

  4. #4
    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: How can I: Know the parent class name...

    Quote Originally Posted by TCB13 View Post
    Well... both... I'm new to this QT/Classes stuff
    It's not both. These are two completely different things, you need to be able to distinguish them to be able to explain what your problem is. Otherwise how can you expect anyone to help you?

    Anyway I need to know in the "class_a" code the name of the class who's calling it...
    So you want the class name of the caller, I guess. You can't know that if you're already in the called function. If the caller was the parent object of the callee, then you could go through Qt's parent() method but it won't work in a general case.
    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.


  5. #5
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I: Know the parent class name...

    Hi,

    Quote Originally Posted by wysota View Post
    It's not both. These are two completely different things, you need to be able to distinguish them to be able to explain what your problem is. Otherwise how can you expect anyone to help you?
    Yes... you're right I asked it too fast it's really a C++ classes question, but I'm looking for a way to do it using QT.

    Quote Originally Posted by wysota View Post
    So you want the class name of the caller, I guess. You can't know that if you're already in the called function. If the caller was the parent object of the callee, then you could go through Qt's parent() method but it won't work in a general case.
    class name of the caller -> Yes

    I've already tried to use that as I said in the first message but I don't really know how to implement it on the constructor.

    Thanks.

  6. #6
    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: How can I: Know the parent class name...

    There is nothing to implement. As I said, in a general case it will not work.
    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.


  7. #7
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I: Know the parent class name...

    Quote Originally Posted by wysota View Post
    There is nothing to implement. As I said, in a general case it will not work.
    So... How should I do it?

    Thanks.

  8. #8
    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: How can I: Know the parent class name...

    You should not do it at all. What do you need this information for?
    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.


  9. #9
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How can I: Know the parent class name...

    There are a number of unpleasant ways to do this. One is to require that a pointer or reference to the caller be passed to the function, then use typeid to determine the type through that parameter. There is no way to guarantee, though, that the caller passes the correct argument, and this approach leads to a sloppy 'if' statement based solution.

    In general, functions have no idea what has called them; if you need to know you are probably formulating the problem incorrectly and need to look for another solution. One approach might be to define the function in an abstract base class and have derived classes implement their own slight variations of it as required. But without a better problem statement it's hard to say.

  10. The following user says thank you to SixDegrees for this useful post:

    TCB13 (8th September 2011)

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I: Know the parent class name...

    Explain what you are trying to achieve by "change the output a little bit to fit the situation" and how that relates to the "name of the parent class". A small code example that demonstrates what you want to achieve would also be useful.

  12. #11
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: How can I: Know the parent class name...

    Quote Originally Posted by SixDegrees View Post
    (...) But without a better problem statement it's hard to say.
    Quote Originally Posted by ChrisW67 View Post
    (...) A small code example that demonstrates what you want to achieve would also be useful.
    Quote Originally Posted by wysota View Post
    You should not do it at all. What do you need this information for?
    First, thanks SixDegrees for the info provided.

    Anyway, I'm using a class over two QT projects because it's a simple HTTP post class it some custom basic authentication in place. The problem is that I need a different User-Agent for each project. (Another extra security measure since I'm also verifying that in my PHP)
    -> Of course I can add another parameter to the class constructor and then use a if statement to decide witch User-Agent should be used.

    But another way to do it would be nice since I'll need to change a lot's of code to make that small change.

    Before you all ask: I'm developing my own security and surveillance system for my 2 houses and I'm using QT to run some TCP data transactions and HTTP posts between fonera boards, arduinos and the main computers...

    Thanks.

  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How can I: Know the parent class name...

    Well, don't change the HTTP Post class constructor. Add a method to set the user agent and give the value a sensible default. Code that doesn't call the new method will get the default UA. Code that is modified to call the new method can set its user agent. If you really want, you can make the variable holding the current UA value static so that all instances of the HTTP Post class (in the same process) share the same value.

    An alternative:
    You could put the user agent member variable in the protected section of your base HTTP Post class, give it a sensible default value in the base class constructor, derive from this base in each project, and override the default value in the derived class constructor. Use the derived class in your project.

  14. The following user says thank you to ChrisW67 for this useful post:

    TCB13 (8th September 2011)

  15. #13
    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: How can I: Know the parent class name...

    Another solution would be to have a virtual method returning the user agent which you could override by subclassing. Then choosing the right class when creating the object would use different user agents.
    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. The following user says thank you to wysota for this useful post:

    TCB13 (8th September 2011)

  17. #14
    Join Date
    Aug 2011
    Location
    Portugal
    Posts
    34
    Thanks
    15
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How can I: Know the parent class name...

    Quote Originally Posted by ChrisW67 View Post
    Well, don't change the HTTP Post class constructor. Add a method to set the user agent and give the value a sensible default. Code that doesn't call the new method will get the default UA. Code that is modified to call the new method can set its user agent. If you really want, you can make the variable holding the current UA value static so that all instances of the HTTP Post class (in the same process) share the same value.
    - Working on that Reading some info about methods.

    Thanks.

Similar Threads

  1. How to access to methods of parent class
    By tonnot in forum General Programming
    Replies: 3
    Last Post: 29th June 2011, 07:04
  2. Replies: 4
    Last Post: 16th March 2011, 17:12
  3. Qt Creator Custom widget plugin being replaced with parent class
    By mhill in forum Qt Tools
    Replies: 0
    Last Post: 28th July 2010, 18:58
  4. Determine Class Type of QObject Parent
    By photo_tom in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2010, 17:42
  5. Replies: 11
    Last Post: 14th March 2010, 22:00

Tags for this Thread

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.