• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 517
  • Last Modified:

Determine return type of function inside the function itself?

Hi,

I'm woundering if it's possible to determine the return type of the function inside the function it self. The reason I need this is because I have a recursive function, which will be called a number of times determined by one of the input arguments. Depending on how many times the function is called, the return type will be different.

As you see in the code I have attached, there is two elements inside the class containing the function, element1 and element2. element1 is an object of same class, but with a different type of element2. I don't know the return type before after I return type before after I enter the function. Is it possible to replace the Object^ return type were I accutally know what type was returned?

Regards
Eimund Smestad
Object^ operator[](unsigned int i) {
  if(i)
    return element1[--i];
  return element2;
}

Open in new window

0
TheEimund
Asked:
TheEimund
  • 3
  • 2
2 Solutions
 
Todd GerbertIT ConsultantCommented:
0
 
Todd GerbertIT ConsultantCommented:
>> Is it possible to replace the Object^ return type were I accutally know what type was returned?

I'm not sure I understand this part of the question...your function will always return a Object^, but in the calling function you can use GetType to determine the returned value's type at run-time.

The point here is that even though MyClass's [] operator always returns an Object^, the function ShowObjectValue is able to determine it's type.

#include "stdafx.h"

using namespace System;

ref class MyClass
{
private:
	array<Object^>^ list;
public:
	MyClass()
	{
		list = gcnew array<Object^>(2);
		list[0] = (int)10;
		list[1] = "Hello World!";
	}

	Object^ operator[](unsigned int i)
	{
		return list[i];
	}
};

void ShowObjectValue(Object^ obj)
{
	Console::WriteLine("The variable is of type {0}, and it's value is: {1}\n",
		obj->GetType(), obj);
}

int main(array<System::String ^> ^args)
{
    MyClass^ obj = gcnew MyClass();
	
	Object^ item1 = obj[0];
	ShowObjectValue(item1);

	Object^ item2 = obj[1];
	ShowObjectValue(item2);

	Console::ReadKey();
    return 0;
}

Open in new window

0
 
Todd GerbertIT ConsultantCommented:
Also see: http://msdn.microsoft.com/en-us/library/kwd9abya.aspx

#include "stdafx.h"

using namespace System;

ref class MyClass
{
private:
	array<Object^>^ list;
public:
	MyClass()
	{
		list = gcnew array<Object^>(2);
		list[0] = (int)10;
		list[1] = "Hello World!";
	}

	Object^ operator[](unsigned int i)
	{
		return list[i];
	}
};

bool IsString(Object^ obj)
{
	if (obj->GetType() == System::String::typeid)
		return true;
	else
		return false;
}

int main(array<System::String ^> ^args)
{
    MyClass^ obj = gcnew MyClass();
	
	Object^ item1 = obj[0];
	Console::WriteLine("item1 is a string: {0}\n", IsString(item1));

	Object^ item2 = obj[1];
	Console::WriteLine("item2 is a string: {0}\n", IsString(item2));

	Console::ReadKey();
    return 0;
}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TheEimundAuthor Commented:
Thank you tgerbert for your insightful reponse! As I understand the the Type class does not help me, maybe I'm wrong. But what I want is something that gives me the actual type and be able to use this as that type. If I have an object named MyCollection and want the the third element which is a double, I want something like this

auto element = MyCollection[2];

where element becomes a double. And simular if the fourth element is a String

auto element = MyCollection[3];

where element becomes a String. Is it possible to typecast a object of Type to the acutal type? Something like this

auto element = dynamic_cast<MyCollection[2]->CastType()>(MyCollection[2]);

where CastType is my imaginary casting function.
0
 
sarabandeCommented:
you should see that if the function could return different return types the calling function also would have a problem.

 
x = container[i];

Open in new window


the x couldn't be different types. on the best it could be a type which could take both element1.at(i) or element2. but if so, why not using that super type as return type?

one more thing: operator[] is supposed to return an element of a container. if you have a container that could take different types at runtime you already would need to have a super type (or baseclass type) for the container. if the operator[] returns elements of multiple containers it most likely is not well-defined. you then better use a function like

   
ReturnType getAt(int i, type1 & t1, type2 & t2);

Open in new window


which either returns t1 or t2 and tell with ReturnType (enumeration) what it returned.

Sara
0
 
TheEimundAuthor Commented:
Thank you for your information. Not quit what I was looking for, but was able to solve my problem anyway.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now