is possible create a any type variable(1 variable that accept any type of values)? - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 42 of 42

Thread: is possible create a any type variable(1 variable that accept any type of values)?

  1. #31
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    Now that you have a c++11 compiler, you might want to revisit where all this started - How can I convert write procedure to cout

    http://forums.codeguru.com/showthrea...cedure-to-cout

    and see my post #15 with the solution using variadic templates. As gcc 4.8.1 supports variadic templates, this code should compile and give you the write function you originally wanted without using the variant class - which I suspect is going to give more trouble than it's worth.

    Code:
    #include <iostream>
    using namespace std;
    
    void write()
    {
       cout << " ";
    }
    
    template <typename A, typename ...B>
    void write(A argHead, B... argTail)
    {
       cout << argHead << " ";
       write(argTail...);
    }
    
    
    int main(void)
    {
       write(1, 2, "qwerty", 4.45, "five", 6);
       return 0;
    }
    thanks.
    now i'm doing the same for read(): but i get errors:
    Code:
    void read()
        {
            cin >> " ";
        }
    
        template <typename A, typename ...B>
            void read(A argHead, B... argTail)
            {
                cin >> argHead >> "";
                read(argTail...);
            }
    don't forget theres these type of read too:
    Code:
    void read()
    	{
    		DWORD oldMode;
    		GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
    		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
    
    		FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
    
    		char buffer[1];
    		DWORD read;
    		ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
    
    		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
    		CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
    	}
    thanks you for all my friend.. thank you both

  2. #32
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Code:
    void read()
        {
            cin >> " ";
        }
    No! You cannot do a stream extraction to a literal string! Just comment out the cin line.

    Code:
    cin >> argHead >> "";
    Again, you cannot do a stream extraction to a literal string.

    Just do
    Code:
    cin >> argHead;
    Also the parameters for read should be passed by reference not by value as you want to pass back the value read.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #33
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    Code:
    void read()
        {
            cin >> " ";
        }
    No! You cannot do a stream extraction to a literal string! Just comment out the cin line.

    Code:
    cin >> argHead >> "";
    Again, you cannot do a stream extraction to a literal string.

    Just do
    Code:
    cin >> argHead;
    Also the parameters for read should be passed by reference not by value as you want to pass back the value read.
    the variable isn't changed
    Code:
    void read()
        {
            //cin;
        }
    
        template <typename A, typename ...B>
            void read(A argHead, B... argTail)
            {
                cin >> argHead ;
                read(&argTail...);
            }

  4. #34
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    now works fine:

    Code:
    	void read()
        {
            //cin;
        }
    
        template <typename A, typename ...B>
            void read(A &argHead, B... argTail)
            {
                cin >> argHead ;
                read(argTail...);
            }
    but if i use these tooo:
    Code:
    void read(void)
    	{
    		DWORD oldMode;
    		GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
    		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
    
    		FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
    
    		char buffer[1];
    		DWORD read;
    		ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
    
    		SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
    		CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
    	}
    i get an error:
    "|184|error: 'void Console::read()' cannot be overloaded|"

    can you advice me?

  5. #35
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Because as part of your variadic template read, you have the function
    Code:
    void read() {}
    so you cannot have another function with the definition void read(void) - which is the same as void read()!

    However, for your ReadConsole read function, it takes no arguments and returns void - so what happens to what it reads and what's its purpose? I would suggest changing its function name to something like readcon(..). Also, why are you closing the handle for standard input?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #36
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    Because as part of your variadic template read, you have the function
    Code:
    void read() {}
    so you cannot have another function with the definition void read(void) - which is the same as void read()!

    However, for your ReadConsole read function, it takes no arguments and returns void - so what happens to what it reads and what's its purpose? I would suggest changing its function name to something like readcon(..). Also, why are you closing the handle for standard input?
    that function is only for wait the user click in enter for continue\exit

  7. #37
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    yes i can do it hehehehe
    seems that both functions are called
    but i simply create a boolean variable and now seems to work....
    see the code:

    Code:
    class Console{
    	private:
    	char pszOldWindowTitle[MY_BUFSIZE];
    	HWND ConsoleHandle;
    	HDC ConsoleDC;
    	bool blnVisible;
            bool ReadEnter=false; //these boolean variable is for distinguish the functions used;)
            ...........................................................................
             
            //see now the functions:
            void read()	{
    	    if (ReadEnter==false)
            {
                DWORD oldMode;
                GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &oldMode);
                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_LINE_INPUT);
    
    
                FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
    
    
                char buffer[1];
                DWORD read;
                ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), buffer, sizeof(buffer), &read, NULL);
    
    
                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), oldMode);
                CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
            }
    	}
    
    
        template <typename A, typename ...B>
            void read(A &argHead, B... argTail)
            {
                ReadEnter=true;
                cin >> argHead ;
                read(argTail...);
                ReadEnter=false;
            }
    and seems to work normaly.... sometimes my brain works more hehehehe
    but tell me what you think
    heres how use them:
    Code:
    Console a;    int c=10;
        a.read(c)  ;
        a.write(c);
        a.read();
    let me ask you 1 last thing about header files. how can i test if the header file was called?
    (for don't give about the function be double declared(or something like it))
    thanks for all my friend.. thanks to both
    (i'm trying rate you, both, but isn't easy)

  8. #38
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: is possible create a any type variable(1 variable that accept any type of values)

    let me ask you 1 last thing about header files. how can i test if the header file was called?
    ??? You don't call a header file. You include a header file to provide the definitions for class/functions etc. If your code uses a class or function defined in a header and the header hasn't been included then the compiler will soon complain.

    If you mean how do you prevent the header file from being included more than once, then there are two ways

    The simplest is to have the statement

    Code:
    #pragma once
    as the first line of the header file. However, not all compilers recognise this. The 'standard' way is to use a 'guard define' like this

    Code:
    #ifndef myheadguard1
    #define myheadguard1
    //insert contents of header here
    #endif
    where myheadguard1 is unique for each header.

    You might also want to consider using a namespace for your class.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #39
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    ??? You don't call a header file. You include a header file to provide the definitions for class/functions etc. If your code uses a class or function defined in a header and the header hasn't been included then the compiler will soon complain.

    If you mean how do you prevent the header file from being included more than once, then there are two ways

    The simplest is to have the statement

    Code:
    #pragma once
    as the first line of the header file. However, not all compilers recognise this. The 'standard' way is to use a 'guard define' like this

    Code:
    #ifndef myheadguard1
    #define myheadguard1
    //insert contents of header here
    #endif
    where myheadguard1 is unique for each header.

    You might also want to consider using a namespace for your class.
    why use a namespace, if is only use 1 class?
    thanks for all

  10. #40
    Join Date
    Apr 1999
    Posts
    27,431

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by Cambalinho View Post
    why use a namespace, if is only use 1 class?
    thanks for all
    It is 1 class now, but what about tomorrow, next month, next year? You don't know the future.

    What if you write new code, and your name clashes with an existing name from another class that may not be yours? For example, what if you created a class called "list", or "string" or "map", etc. and you now #include a standard header that just happens to have classes with the same name?

    Regards,

    Paul McKenzie

  11. #41
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,455

    Re: is possible create a any type variable(1 variable that accept any type of values)

    seems that both functions are called
    but i simply create a boolean variable and now seems to work....
    and seems to work normaly.... sometimes my brain works more
    but tell me what you think
    I would advise caution about mixing c++ io streaming (using cin, cout etc) with the low-level console functions (ReadConsole etc) - especially on input as 'strange' results can happen if the streaming io buffers are not empty before using the ReadConsole/WriteConsole group of functions.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #42
    Join Date
    Apr 2009
    Posts
    660

    Re: is possible create a any type variable(1 variable that accept any type of values)

    Quote Originally Posted by 2kaud View Post
    I would advise caution about mixing c++ io streaming (using cin, cout etc) with the low-level console functions (ReadConsole etc) - especially on input as 'strange' results can happen if the streaming io buffers are not empty before using the ReadConsole/WriteConsole group of functions.
    so what is the best way for clean the streaming io buffers?
    (maybe that's why the getch(), sometimes, is ignored)

Page 3 of 3 FirstFirst 123

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center