Thread: Storing classes in variables?

  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
    28,413
    It sounds like you want to write a template, possibly a function template.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,149
    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