Regardless of whether or not this is a good idea, is it possible to implement an interface where the executing function is aware of the calling object's type?
class A
{
private C;
public int doC(int input)
{
return C.DoSomething(input);
}
}
class B
{
private C;
public int doC(int input)
{
return C.DoSomething(input);
}
}
class C
{
public int DoSomething(int input)
{
if(GetType(CallingObject) == A)
{
return input + 1;
}
else if(GetType(CallingObject) == B)
{
return input + 2;
}
else
{
return input + 3;
}
}
}
It seems to me like this is a bad coding practice (because the parameters don't change, but the output would) but aside from that is it possible?
I'm in a situation were I want a few specific types to be able to call a certain function, but I can't exclude access to the function. I thought about having a "type" parameter
DoSomething(int input, Type callingobject)
But there's no guarantee that the calling object would use GetType(this), as opposed to GetType(B) to spoof a B regardless of their own type.
Would this be as simple (relatively simple) as examining the callstack?
Get and print the type of an object: type() type() returns the type of an object. You can use this to get and print the type of a variable and a literal like typeof in other programming languages. The return value of type() is type object such as str or int .
The typeid keyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program.
Calling an object's method is similar to getting an object's variable. To call an object's method, simply append the method name to an object reference with an intervening '. ' (period), and provide any arguments to the method within enclosing parentheses.
C++ has no direct method to check one object is an instance of some class type or not. In Java, we can get this kind of facility. In C++11, we can find one item called is_base_of<Base, T>. This will check if the given class is a base of the given object or not.
First, yes, it's a terrible idea to do this and breaks all kinds of solid design principles. You should definitely consider an alternative approach if that's open, like simply using polymorphism—this seems like it can be refactored to a pretty clear case of single dispatch.
Secondly, yes, it's possible. Use System.Diagnostics.StackTrace
to walk the stack; then get the appropriate StackFrame
one level up. Then determine which method was the caller by using GetMethod()
on that StackFrame
. Note that building a stack trace is a potentially expensive operation, and it's possible for callers of your method to obscure where things are really coming from.
Edit: This comment from the OP makes it pretty clear that this could probably be a generic or polymorphic method. @devinb, you might want to consider making a new question that provides more detail about what you're trying to do, and we can see if it lends itself well to a good solution.
The short version is that I would end up have 30 or 40 identical functions that were simply off by one or two lines. – devinb (12 secs ago)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With