Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > passing classes to functions, not objects

Reply
Thread Tools

passing classes to functions, not objects

 
 
Patrick Stinson
Guest
Posts: n/a
 
      09-06-2004
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!
 
Reply With Quote
 
 
 
 
Unforgiven
Guest
Posts: n/a
 
      09-06-2004
"Patrick Stinson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
Dave Townsend
Guest
Posts: n/a
 
      09-06-2004

"Unforgiven" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Patrick Stinson" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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




 
Reply With Quote
 
Aguilar, James
Guest
Posts: n/a
 
      09-07-2004
"Patrick Stinson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Risto Lankinen
Guest
Posts: n/a
 
      09-07-2004

"Patrick Stinson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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 -


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-07-2004
> 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
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      09-08-2004
In message <RUl%c.26778$(E-Mail Removed)>, JKop <(E-Mail Removed)>
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-08-2004

> 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
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      09-09-2004
In message <qLG%c.26844$(E-Mail Removed)>, JKop <(E-Mail Removed)>
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
 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      09-09-2004
Richard Herring posted:

> In message <qLG%c.26844$(E-Mail Removed)>, JKop

<(E-Mail Removed)>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data between objects and calling all objects of a class in turn ghoetker Python 1 08-25-2010 03:18 AM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
How can I create instantiable objects (not classes)? Julien Perl Misc 3 11-18-2006 03:32 AM
Passing Classes as Arguments to Functions of Other Classes crouse@physics.wm.edu Java 7 08-12-2006 02:08 AM
Which objects or classes cannot be serialized? 'not serializable' error Will Java 3 02-19-2006 11:27 AM



Advertisments