PDA

View Full Version : How can I: Know the class name of the caller...



TCB13
7th September 2011, 22:48
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.

wysota
7th September 2011, 22:55
Is it a Qt question or a C++ question? And by "parent class" do you mean a "super class" or a "parent object class"?

TCB13
7th September 2011, 23:28
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...

wysota
7th September 2011, 23:50
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.

TCB13
8th September 2011, 00:09
Hi,


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.


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.

wysota
8th September 2011, 00:30
There is nothing to implement. As I said, in a general case it will not work.

TCB13
8th September 2011, 00:35
There is nothing to implement. As I said, in a general case it will not work.

So... How should I do it?

Thanks.

wysota
8th September 2011, 00:55
You should not do it at all. What do you need this information for?

SixDegrees
8th September 2011, 00:58
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.

ChrisW67
8th September 2011, 00:58
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.

TCB13
8th September 2011, 02:41
(...) But without a better problem statement it's hard to say.

(...) A small code example that demonstrates what you want to achieve would also be useful.

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.

ChrisW67
8th September 2011, 05:54
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.

wysota
8th September 2011, 10:42
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.

TCB13
8th September 2011, 13:23
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.