Click to See Complete Forum and Search --> : A question on Knowing the data type


Paramjeet Singh
September 20th, 2002, 01:37 AM
hi friends
I want to know the data type for a particular scenario . If anyone is aware please do reply.

example

class a
{
public:
func();
private:
char* ptr1;
wchar_t* ptr2;
};

void main()
{
a obj("Test");
char* ptr = func();

}

Explaination :
I have a class 'a' as shown above. The class can have any of the 2 pointers shown above allocated during its lifetime. The func() function in the class shown above and which is not implemented returns either a char* or a wchar_t* as per demanded by the users call to the function.

So if somebody can tell me how do i know what the user has asked for i.e. has he asked for a char* or a wchar_t*. Then knowing that i should return the specific pointer and the return type also should be dynamic.

Thanks
Paramjeet

cup
September 20th, 2002, 02:34 AM
There can only be one return type for a function but it can have parameters of different types.

...
void func (char*& result)
{
result = ptr1;
}

void func(wchar_t*& result)
{
result = ptr2;
}
...
char* ptr;
wchar_t* wptr;
a.func (ptr); // calls the char* version
a.func (wptr); // calls the wchar_t* version

Paramjeet Singh
September 20th, 2002, 04:11 AM
That is what i know. But i wanted some way by the type can be known. I wanted to know what the user is asking for a char* or wchar_t*

Originally posted by cup
There can only be one return type for a function but it can have parameters of different types.

...
void func (char*& result)
{
result = ptr1;
}

void func(wchar_t*& result)
{
result = ptr2;
}
...
char* ptr;
wchar_t* wptr;
a.func (ptr); // calls the char* version
a.func (wptr); // calls the wchar_t* version

Ungi
September 20th, 2002, 04:14 AM
You know it because of the function that is called.

...
void func (char*& result)
{
//Type is char*
result = ptr1;
}

void func(wchar_t*& result)
{
//Type is wchar_t*
result = ptr2;
}
...
char* ptr;
wchar_t* wptr;
a.func (ptr); // calls the char* version
a.func (wptr); // calls the wchar_t* version