473,386 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

passing classes to functions, not objects

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!
Jul 22 '05 #1
10 6618
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
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

Jul 22 '05 #2

"Unforgiven" <ja*******@hotmail.com> wrote in message
news:2q************@uni-berlin.de...
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
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


Jul 22 '05 #3
"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
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.
Jul 22 '05 #4

"Patrick Stinson" <aj*****@gci.net> wrote in message
news:10*************@corp.supernews.com...
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 -
Jul 22 '05 #5
> 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
Jul 22 '05 #6
In message <RU******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
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
Jul 22 '05 #7
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
Jul 22 '05 #8
In message <qL******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
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
Jul 22 '05 #9
Richard Herring posted:
In message <qL******************@news.indigo.ie>, JKop <NU**@NULL.NULL> 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'relooking 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
Jul 22 '05 #10
In message <BZ******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
Richard Herring posted:
In message <qL******************@news.indigo.ie>, JKop<NU**@NULL.NULL>
writes
[quoting me - please attribute your quotes]

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

Because one has to look through the Standard and findwhat they'relooking 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
Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
6
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among...
45
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
4
by: mharness | last post by:
Hello All, I've defined a number of classes that currently only include public variable declarations. I've been reluctant to add subroutines and functions for fear of taking a performance hit...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
3
by: Subodh | last post by:
Hi All, In C++ we could pass a constant reference to a function so that the target function could not modify the objects passed eg. const classA & dummmyfunction(const classB) similar thing...
23
by: osama178 | last post by:
Let's say I have this code -------------------- class GenericQueue { public: bool Pop(void*& refToPtr); // //--------------------(1) };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.