passing classes to functions, not objects

  • Thread starter Patrick Stinson
  • Start date
P

Patrick Stinson

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!
 
U

Unforgiven

Patrick Stinson said:
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.
 
D

Dave Townsend

Unforgiven said:
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.

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
 
A

Aguilar, James

Patrick Stinson said:
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.
 
R

Risto Lankinen

Patrick Stinson said:
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 -
 
J

JKop

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
 
R

Richard Herring

[QUOTE="JKop said:
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[/QUOTE]

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);
 
J

JKop

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
 
R

Richard Herring

In message <qLG%[email protected]>, JKop <[email protected]>
writes
[quoting me - please attribute your quotes]
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...
 
J

JKop

Richard Herring posted:
In message <qLG%[email protected]>, JKop
writes
[quoting me - please attribute your quotes]
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?
Prospective employers take note...

OFF-TOPIC


-JKop
 
R

Richard Herring

In message <qLG%[email protected]>, 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?[/QUOTE]

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.)
OFF-TOPIC

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top