473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6651
"Patrick Stinson" <aj*****@gci.ne t> wrote in message
news:10******** *****@corp.supe rnews.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*******@hotm ail.com> wrote in message
news:2q******** ****@uni-berlin.de...
"Patrick Stinson" <aj*****@gci.ne t> wrote in message
news:10******** *****@corp.supe rnews.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.ne t> wrote in message
news:10******** *****@corp.supe rnews.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.ne t> wrote in message
news:10******** *****@corp.supe rnews.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.nam e()
<< "\n\nBytes occupied in memory: "
<< sizeof(T) << '\n';
}

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

ExamineType<Som eClass>();

ExamineType<Bla h>();

ClassName object_name;

ExamineType<obj ect_name>();
//I think you can give it an object as in the above
}
-JKop
Jul 22 '05 #6
In message <RU************ ******@news.ind igo.ie>, JKop <NU**@NULL.NULL >
writes
Is there a way to pass a class as a
parameter to a function?
Untested code:

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

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

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

ExamineType<Som eClass>();

ExamineType<Bla h>();

ClassName object_name;

ExamineType<obj ect_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(obj ect_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.ind igo.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.ind igo.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

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

Similar topics

3
14947
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
1
741
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 managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
6
2305
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 several other objects, I'd like to know how this is handled commonly. Could someone elaborate on this? Another situation is some objects could be shared among several threads. How to handle it commonly?
45
3618
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 themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
9
4804
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 am using vector class(STL), the compiler does not allow me to do this. I do realize there is a pitfall in this approach(size of arrays not matching etc), but I wonder how to get around this problem. I have a class hierachy with abstract base...
39
7663
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 indicate: a) That I don't know enough b) Passing arguments by ref is bad
4
1383
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 when I pass the class as an argument to another function and in a one case, store the class to a session variable. Right now I have all of my functions and subroutines in a single class that's becoming a bit unwieldy. What I'd like to do is move...
12
5342
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 the custom employee class and have built it as a separate library (employee.dll). This employee.dll is being referenced by both the web service and the windows application. I face the following problem when I send this class to the webservice.
3
5468
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 was valid for objects passed with pointer to constant objects In C++/CLI is there any way for imitating this, I want to pass arguments to and return value from my member function as a constant
23
2148
by: osama178 | last post by:
Let's say I have this code -------------------- class GenericQueue { public: bool Pop(void*& refToPtr); // //--------------------(1) };
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.