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! 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
"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
"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.
"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 -
> 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
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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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);
|
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...
|
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?
|
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...
|
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...
| |
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
|
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...
|
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.
|
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
|
by: osama178 |
last post by:
Let's say I have this code
--------------------
class GenericQueue
{
public:
bool Pop(void*& refToPtr); // //--------------------(1)
};
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| | |