Storing classes in variables?

This is a discussion on Storing classes in variables? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to store a class in a variable, so that for example, code like this would work? ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Storing classes in variables?

    Is there a way to store a class in a variable, so that for example, code like this would work?

    Code:
    int main() {
        T = vector;
        T v;
        v.push_back(0);
        T = list;
        T l;
        l.push_back(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    It sounds like you want to write a template, possibly a function template.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    For your question, no. For a solution other than a generic answer, you must be more specific about what you're trying to do.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    jw232 -

    Look into the "typedef" keyword.
    http://www.cprogramming.com/tutorial/typedef.html

    This may be what you are looking for. It does not use the syntax of
    Code:
    T = vector;
    But rather
    Code:
    typedef vector T;
    T v;
    v.push_back(0);
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by neandrake View Post
    jw232 -

    Look into the "typedef" keyword.
    http://www.cprogramming.com/tutorial/typedef.html
    That's what I was about to suggest, but then I saw he wanted to reassign a different type to T later, which can't be done with typedefs.
    I believe the functionality you're looking for is something like the Boost Any class or Variant class; although the syntax of using them would be quite different than what was posted.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by cpjust View Post
    That's what I was about to suggest, but then I saw he wanted to reassign a different type to T later, which can't be done with typedefs.
    I believe the functionality you're looking for is something like the Boost Any class or Variant class; although the syntax of using them would be quite different than what was posted.
    I totally missed that he wanted to reassign T. Nice catch.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Using proper Object Oriented techniques, you should be able to accomplish this with an interface, right?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by neandrake
    Using proper Object Oriented techniques, you should be able to accomplish this with an interface, right?
    If by "interface" you mean inheritance and polymorphism with an abstract base class that is a pure interface, then probably yes. It really depends on what jw232 actually wants to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by laserlight View Post
    If by "interface" you mean inheritance and polymorphism with an abstract base class that is a pure interface, then probably yes. It really depends on what jw232 actually wants to do.
    Tis what I meant
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    prog-bman: exactly. same thing as his description, just a different syntax. or you can use an interface/adapter/etc. my point was how to accomplish his goal. perhaps if he elaborated at the bigger issue he is trying to solve, there might be a better means.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Boost Any class or Variant class don't really support this kind of thing.

    I agree that interfaces are generally a good way to do this. However, these are intrusive to the types that can be assigned, so you would have to wrap library classes and methods in your own classes, to get it to work.

    The ideal way to do this is to have a type-class that tells you that list and vector both have compatible push_back() methods, so that you can have a common interface to them without a class wrapper. There would still be separate code that says that the two are similar, but it would not require unwrapping the object to use it in a different way. This feature is coming up in the form of Concepts in C++0x.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes as member variables
    By Stonehambey in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2008, 07:01 PM
  2. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  3. global variables
    By shadovv in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2005, 02:21 PM
  4. Classes, dynamic variables, and leaks.
    By Grins2Pain in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 03:07 PM
  5. 2 questions - classes and variables
    By phantom in forum C++ Programming
    Replies: 8
    Last Post: 09-25-2001, 09:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21