passing classes to functions, not objects

Discussion in 'C++' started by Patrick Stinson, Sep 6, 2004.

  1. What sort of operations can one do with classes? I know

    class A{};
    typeid(A);

    works, but what else can you do? Is there a way to pass a class as a
    parameter to a function? class pointers? how does this relate to

    class A{};
    void (A::*)();

    I'm looking for more of a quasi-comprehensive discussion. Cheers!
    Patrick Stinson, Sep 6, 2004
    #1
    1. Advertising

  2. Patrick Stinson

    Unforgiven Guest

    "Patrick Stinson" <> wrote in message
    news:...
    > What sort of operations can one do with classes? I know
    >
    > class A{};
    > typeid(A);
    >
    > works, but what else can you do? Is there a way to pass a class as a
    > parameter to a function? class pointers?


    Not that I know of. Template parameters might serve to something in that
    effect, but it has to be determined at compile time.
    The question is what you want to do with your hypothetical class pointers.
    Instantiate the class? Call (possibly static) methods on it? To do that kind
    of thing at runtime, some more extensive RTTI is needed than what C++
    currently provides. If you'd want to find out about the members of a type,
    their parameters, and then call them, all at runtime, some form of
    reflection is needed, and C++ doesn't provide that at all.

    --
    Unforgiven
    Unforgiven, Sep 7, 2004
    #2
    1. Advertising

  3. "Unforgiven" <> wrote in message
    news:...
    > "Patrick Stinson" <> wrote in message
    > news:...
    > > What sort of operations can one do with classes? I know
    > >
    > > class A{};
    > > typeid(A);
    > >
    > > works, but what else can you do? Is there a way to pass a class as a
    > > parameter to a function? class pointers?

    >
    > Not that I know of. Template parameters might serve to something in that
    > effect, but it has to be determined at compile time.
    > The question is what you want to do with your hypothetical class pointers.
    > Instantiate the class? Call (possibly static) methods on it? To do that

    kind
    > of thing at runtime, some more extensive RTTI is needed than what C++
    > currently provides. If you'd want to find out about the members of a type,
    > their parameters, and then call them, all at runtime, some form of
    > reflection is needed, and C++ doesn't provide that at all.
    >
    > --
    > Unforgiven
    >


    This is C++ not Java, in C++ classes are not objects, so you don't have
    pointers
    to classes. The closest you can get is through the use of templates, by
    creating
    a template with a class parameter you can capture some of the type
    information
    about the class as an object. However, this is all compile time knowledge.

    What particular problem were you concerned with?

    dave
    Dave Townsend, Sep 7, 2004
    #3
  4. "Patrick Stinson" <> wrote in message
    news:...
    > What sort of operations can one do with classes? I know
    >
    > class A{};
    > typeid(A);
    >
    > works, but what else can you do? Is there a way to pass a class as a
    > parameter to a function? class pointers? how does this relate to
    >
    > class A{};
    > void (A::*)();
    >
    > I'm looking for more of a quasi-comprehensive discussion. Cheers!


    You could design your own set of reflection classes. It would not be easy,
    but it seems like it should be doable . . . maybe. You might have to
    reimplement the whole language in the process, though. Actually, the
    undergrad research group I am with here at school is doing that kind of
    thing, only with Java. We're extending all of the Java reflection classes
    and doing some pretty nasty/crazy stuff with it. I'm a little early in my
    education to be of any real help with the effort though, so in this, you
    would have to look elsewhere for advice.

    Just remember the first rule of computer science: "Adding a level of
    indirection can solve any problem." <-- This is half tongue in cheek, half
    serious.
    Aguilar, James, Sep 7, 2004
    #4
  5. "Patrick Stinson" <> wrote in message
    news:...
    > What sort of operations can one do with classes? I know
    >
    > class A{};
    > typeid(A);
    >
    > works, but what else can you do?


    You can also use sizeof(A); , and use the scope resolution operator
    to manipulate static members. I think that's about all.

    - Risto -
    Risto Lankinen, Sep 7, 2004
    #5
  6. Patrick Stinson

    JKop Guest

    > Is there a way to pass a class as a
    > parameter to a function?


    Untested code:

    template<class T>
    void ExamineType()
    {
    const type_info& information = typeid(T);

    cout << "======================"
    "|| Type Information ||
    "======================\n\n"
    "Name: "
    << information.name()
    << "\n\nBytes occupied in memory: "
    << sizeof(T) << '\n';
    }

    int main()
    {
    ExamineType<int>();

    ExamineType<SomeClass>();

    ExamineType<Blah>();

    ClassName object_name;

    ExamineType<object_name>();
    //I think you can give it an object as in the above
    }


    -JKop
    JKop, Sep 7, 2004
    #6
  7. In message <RUl%c.26778$>, JKop <>
    writes
    >> Is there a way to pass a class as a
    >> parameter to a function?

    >
    >Untested code:
    >
    >template<class T>
    >void ExamineType()
    >{
    > const type_info& information = typeid(T);
    >
    > cout << "======================"
    > "|| Type Information ||
    > "======================\n\n"
    > "Name: "
    > << information.name()
    > << "\n\nBytes occupied in memory: "
    > << sizeof(T) << '\n';
    >}
    >
    >int main()
    >{
    > ExamineType<int>();
    >
    > ExamineType<SomeClass>();
    >
    > ExamineType<Blah>();
    >
    > ClassName object_name;
    >
    > ExamineType<object_name>();
    > //I think you can give it an object as in the above


    You *think*? Why speculate when a standard exists? 14.3 says you're
    wrong: the template declaration declares the template parameter as a
    type, so the corresponding argument also has to be a type.

    But you can use template argument deduction to get the type of an
    object, and pass that to your function:

    template <typename T>
    void ExamineType(T const &)
    { ExamineType<T>(); }

    ....

    ExamineType(object_name);

    >}
    >


    --
    Richard Herring
    Richard Herring, Sep 8, 2004
    #7
  8. Patrick Stinson

    JKop Guest


    > You *think*? Why speculate when a standard exists?



    Because one has to look through the Standard and find what
    they're looking for. One must be bothered to do so.

    This particular person isn't. Pay me and I will. €30 will
    suffice.


    -JKop
    JKop, Sep 8, 2004
    #8
  9. In message <qLG%c.26844$>, JKop <>
    writes
    [quoting me - please attribute your quotes]
    >
    >> You *think*? Why speculate when a standard exists?

    >
    >Because one has to look through the Standard and find what
    >they're looking for. One must be bothered to do so.


    Yet "one" was bothered enough to post "one's" uninformed (and wrong)
    speculation to a newsgroup which prefers facts to what "one" thinks.
    >
    >This particular person isn't.


    Prospective employers take note...

    > Pay me and I will. €30 will
    >suffice.



    --
    Richard Herring
    Richard Herring, Sep 9, 2004
    #9
  10. Patrick Stinson

    JKop Guest

    Richard Herring posted:

    > In message <qLG%c.26844$>, JKop

    <>
    > writes
    > [quoting me - please attribute your quotes]
    >>
    >>> You *think*? Why speculate when a standard exists?

    >>
    >>Because one has to look through the Standard and find

    what they're
    >>looking for. One must be bothered to do so.

    >
    > Yet "one" was bothered enough to post "one's" uninformed

    (and wrong)
    > speculation to a newsgroup which prefers facts to what

    "one" thinks.

    Are you suggesting that you can stipulate on exactly what I
    am/would be bothered doing?

    >>
    >>This particular person isn't.

    >
    > Prospective employers take note...


    OFF-TOPIC


    -JKop
    JKop, Sep 9, 2004
    #10
  11. In message <BZ%%c.26928$>, JKop <>
    writes
    >Richard Herring posted:
    >
    >> In message <qLG%c.26844$>, JKop

    ><>
    >> writes
    >> [quoting me - please attribute your quotes]
    >>>
    >>>> You *think*? Why speculate when a standard exists?
    >>>
    >>>Because one has to look through the Standard and find

    >what they're
    >>>looking for. One must be bothered to do so.

    >>
    >> Yet "one" was bothered enough to post "one's" uninformed

    >(and wrong)
    >> speculation to a newsgroup which prefers facts to what

    >"one" thinks.
    >
    >Are you suggesting that you can stipulate on exactly what I
    >am/would be bothered doing?


    No. Where did you get that idea?

    I'm saying that your uninformed speculations are of no interest to
    people seeking factual information about standard C++.

    (I'm also wondering why you made such a fuss about getting hold of a
    copy of the standard without paying for it, if you aren't going to use
    it.)
    >
    >>>
    >>>This particular person isn't.

    >>
    >> Prospective employers take note...

    >
    >OFF-TOPIC


    Well, they were *your* uninformed speculations, and you're too late to
    retract them. Your effusions are all in the archives.

    --
    Richard Herring
    Richard Herring, Sep 10, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Razvan
    Replies:
    11
    Views:
    893
    Andrew Thompson
    Jul 17, 2004
  2. ILLOGIC
    Replies:
    1
    Views:
    349
    Rob Williscroft
    Jun 1, 2004
  3. Steven T. Hatton

    Passing member functions to C functions?

    Steven T. Hatton, Oct 4, 2004, in forum: C++
    Replies:
    7
    Views:
    1,303
    David Hilsee
    Oct 7, 2004
  4. Daniel Aarno
    Replies:
    5
    Views:
    412
    Alf P. Steinbach
    Oct 8, 2004
  5. Replies:
    7
    Views:
    545
    Alex Hunsley
    Aug 12, 2006
Loading...

Share This Page