CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2002
    Posts
    2

    A question on Knowing the data type

    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

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    There can only be one return type for a function but it can have parameters of different types.
    Code:
    ...
    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
    Succinct is verbose for terse

  3. #3
    Join Date
    Sep 2002
    Posts
    2
    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.
    Code:
    ...
    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

  4. #4
    Join Date
    Apr 2002
    Posts
    388
    You know it because of the function that is called.
    Code:
    ...
    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
    mfg Ungi

    Music, music and VB. VB is like music: You never know how it is interpreted.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured