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

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

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    605

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

    can i create 1 variable that accept any type?
    and can i give it the NULL value too?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

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

    It depends on what exactly you are trying to do. Take a look at Boost.Any for some inspiration.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 2009
    Posts
    605

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

    cool.. seems that i can do these:
    Code:
    void Write(void *var1,void *var2=NULL, void *var3=NULL)
    	{
    		cout << var1 << var2 << var3;
    	}
    i just thot do it. and no errors
    thanks

  4. #4
    Join Date
    Apr 2009
    Posts
    605

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

    well works fine for a while.. now don't

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

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

    No compile error and "works as desired" isn't the same thing.

    You have discovered that it is possible to print a pointer to void. This is useful when you want to print an address. However, it is not the same thing as printing a "variable that accept any type of values".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by laserlight View Post
    No compile error and "works as desired" isn't the same thing.

    You have discovered that it is possible to print a pointer to void. This is useful when you want to print an address. However, it is not the same thing as printing a "variable that accept any type of values".
    true. that's why works in begining and now don't

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    Quote Originally Posted by Cambalinho View Post
    can i create 1 variable that accept any type?
    and can i give it the NULL value too?
    Giving a variable a NULL value is really only done for pointers. Via casting, a pointer variable can be specified to point to anything - but this is not really recommended!

    In c++11, an auto variable can be defined to take the type of the result of the RHS of an assignment, but once defined it retains its type whilst in scope.

    See http://msdn.microsoft.com/en-us/library/dd293667.aspx
    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.

  8. #8
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    Giving a variable a NULL value is really only done for pointers. Via casting, a pointer variable can be specified to point to anything - but this is not really recommended!

    In c++11, an auto variable can be defined to take the type of the result of the RHS of an assignment, but once defined it retains its type whilst in scope.

    See http://msdn.microsoft.com/en-us/library/dd293667.aspx
    i found something but isn't compatible with Dev C++

    Code:
    // In header: <boost/any.hpp>
    
    
    class any {
    public:
      // construct/copy/destruct
      any();
      any(const any &);
      any(any &&);//i get several errors inclued these line:(
      template<typename ValueType> any(const ValueType &);
      template<typename ValueType> any(ValueType &&);
      any & operator=(const any &);
      any & operator=(any &&);
      template<typename ValueType> any & operator=(const ValueType &);
      template<typename ValueType> any & operator=(ValueType &&);
      ~any();
    
      // modifiers
      any & swap(any &);
    
      // queries
      bool empty() const;
      const std::type_info & type() const;
    };

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

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

    Quote Originally Posted by Cambalinho View Post
    i found something but isn't compatible with Dev C++

    Code:
    // In header: <boost/any.hpp>
    
    
    class any {
    public:
      // construct/copy/destruct
      any();
      any(const any &);
      any(any &&);//i get several errors inclued these line:(
      template<typename ValueType> any(const ValueType &);
      template<typename ValueType> any(ValueType &&);
      any & operator=(const any &);
      any & operator=(any &&);
      template<typename ValueType> any & operator=(const ValueType &);
      template<typename ValueType> any & operator=(ValueType &&);
      ~any();
    
      // modifiers
      any & swap(any &);
    
      // queries
      bool empty() const;
      const std::type_info & type() const;
    };
    That's not what is shown in my copy of boost/any.hpp

    Code:
    any(any &&);
    Is not valid c++! So you woudn't find this in a boost file!
    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.

  10. #10
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    That's not what is shown in my copy of boost/any.hpp

    Code:
    any(any &&);
    Is not valid c++! So you woudn't find this in a boost file!
    i found it: http://www.boost.org/doc/libs/1_54_0/boost/any.hpp
    but can i save it .h?

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

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

    i found something but isn't compatible with Dev C++
    As per a previous post, using Dev c++ is not recommended

    http://clicktobegin.net/programming/...dnt-use-dev-c/
    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. #12
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by 2kaud View Post
    As per a previous post, using Dev c++ is not recommended

    http://clicktobegin.net/programming/...dnt-use-dev-c/
    i'm using it, because i need the compiler exe from MinGW32
    if i can use the VS compiler or other IDE it's more easy for me
    Last edited by Cambalinho; August 24th, 2013 at 06:56 AM.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    i'm using it, because i need the compiler exe from MinGW32
    I've mentioned to you on other threads that the compiler used by Dev-C++ and MingW is what is important. That compiler happens to be gcc.

    The gcc compiler already supports everything that you are looking for.

    http://gcc.gnu.org/projects/cxx0x.html

    MingW also uses gcc to compile projects. Dev-C++ (once again) is an IDE that just happens to have an old version of gcc already plugged into it. If you take a look at the link I gave you above, and do a little searching on the Internet, you should be able to upgrade MingW to use the latest version of gcc.

    The boost libraries are extensively tested with gcc and Visual Studio, and I would say the two platforms that must compile successfully before being accepted by boost.org.

    As to IDE's, there is CodeBlocks, which is up-to-date. Do an Intenet search and you will find other IDE's.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Apr 2009
    Posts
    605

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

    Quote Originally Posted by Paul McKenzie View Post
    I've mentioned to you on other threads that the compiler used by Dev-C++ and MingW is what is important. That compiler happens to be gcc.

    The gcc compiler already supports everything that you are looking for.

    http://gcc.gnu.org/projects/cxx0x.html

    MingW also uses gcc to compile projects. Dev-C++ (once again) is an IDE that just happens to have an old version of gcc already plugged into it. If you take a look at the link I gave you above, and do a little searching on the Internet, you should be able to upgrade MingW to use the latest version of gcc.

    The boost libraries are extensively tested with gcc and Visual Studio, and I would say the two platforms that must compile successfully before being accepted by boost.org.

    As to IDE's, there is CodeBlocks, which is up-to-date. Do an Intenet search and you will find other IDE's.

    Regards,

    Paul McKenzie
    seems that i didn't understand that before..thanks
    1 thing: the CodeBlocks suports C++11?

  15. #15
    Join Date
    Apr 1999
    Posts
    27,423

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

    Quote Originally Posted by Cambalinho View Post
    seems that i didn't understand that before..thanks
    1 thing: the CodeBlocks suports C++11?
    You're still confused.

    CodeBlocks is an Integrated Development Environment. It is not a compiler. CodeBlocks, Dev-C++, and even Visual Studio do not know how to compile C++ source code. What does know how to compile C++ source code is the underlying compiler that is used in these programs.

    For CodeBlocks, you can basically plug any compiler into it, but most of the time, people configure CodeBlocks to use MingW or gcc. The latest version of gcc is 4.8 (I believe), and it supports all that have been mentioned to you so far.

    For Dev-C++, it was designed for gcc 3.x, which is close to 10 years old and out-of-date. Version 3.x of gcc is ok if you're purely interested in only C++ 98 standard, but anything beyond that, 3.x is not going to help you.

    For Visual Studio, the compiler used is cl.exe (and cl.exe is being updated to keep up to the current standard (C++ 11). I've heard of Intel compiler being able to be plugged into the Visual Studio IDE, but have never tried it.

    Basically what I'm saying to you is that every IDE that I know of uses 2 or 3 executables:

    1) The compiler that compiles the source code to object code (gcc.exe, cl.exe, etc.)
    2) The linker program that links the various object code (ld.exe, link.exe, etc.)
    3) A debugger program or module that allows you to debug the program (gdb.exe, ddd.exe, Visual Studio's integrated debugger, etc.).

    Optionally, an IDE may just build a "make" file and call a program that knows how to read the make file and build the program.

    Except for 3), where the IDE needs to understand the debugger symbols and position the debugger indicator on the correct line, items 1) and 2) requires just calling the executable that compiles and then links.

    As a matter of fact, you don't even need an IDE to develop a program. The reason why IDE's are used is that using the command-line for very large projects becomes very difficult to maintain (unless you have batch files to compile and link large projects, and some people/companies do just that, and that is to create batch files/make files and forget about IDE's to build their programs).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; August 24th, 2013 at 06:15 PM.

Page 1 of 3 123 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center