whats the best method. delphi lets you do something very simple
ptrMethodFoo : procedure
how do you do this in C++? 6 7922
xsist10 <xs*****@somewh ere.com> wrote in message
news:10******** ******@skink.ru .ac.za... whats the best method. delphi lets you do something very simple
ptrMethodFoo : procedure
how do you do this in C++?
You might need to ask the question more precisely. I'm only guessing what
you want.
class C
{
public:
void f();
void g();
};
int main()
{
C c;
void (C::*p)() = &C::f;
(c.*p)(); // calls C::f
p = &C::g;
(c.*p)(); // calls C::g
};
Here, 'p' is a pointer to a void non-static member function of C that takes
no parameters.
DW
"xsist10" <xs*****@somewh ere.com> wrote in message
news:10******** ******@skink.ru .ac.za... whats the best method.
Best for what?
delphi lets you do something very simple
Delphi is not C++.
ptrMethodFoo : procedure
how do you do this in C++?
#include <iostream>
class C
{
public:
void memberfunc()
{
std::cout << "Hello\n";
}
};
int main()
{
void (C::*p)() = &C::memberfu nc;
(C().*p)(); /* prints "Hello" */
return 0;
}
What are you specifically trying to do?
Which C++ book(s) are you reading?
-Mike
I was trying to do something similar where I have several derived
classes each containing it's own specific member variables.
I am trying to construct a report consisting of values extracted from my
derived classes, where the report is entered as XML and I just extract
custom tags and replace them with a value. The way I wanted to do this
was to construct a list of name-value pairs, where the name is the tag I
am searching for and the value is a pointer to a method from one of the
derived classes. I do not know which classes the values will be coming
from until I reach the desired tag.
How would/should I declare a structure that will simply store a string
value (char *) and a function * as required so I couold call a specific
class method based on one of these string values?
Any help would be appreciated.
Thanks in advance
Woodster
In article <Ek************ ****@newsread4. news.pas.earthl ink.net>, mk******@mkwahl er.net says... "xsist10" <xs*****@somewh ere.com> wrote in message news:10******** ******@skink.ru .ac.za... whats the best method.
Best for what?
delphi lets you do something very simple
Delphi is not C++.
ptrMethodFoo : procedure
how do you do this in C++?
#include <iostream>
class C { public: void memberfunc() { std::cout << "Hello\n"; } };
int main() { void (C::*p)() = &C::memberfu nc; (C().*p)(); /* prints "Hello" */ return 0; } What are you specifically trying to do?
Which C++ book(s) are you reading?
-Mike
Woodster wrote: I was trying to do something similar where I have several derived classes each containing it's own specific member variables.
I am trying to construct a report consisting of values extracted from my derived classes, where the report is entered as XML and I just extract custom tags and replace them with a value. The way I wanted to do this was to construct a list of name-value pairs, where the name is the tag I am searching for and the value is a pointer to a method from one of the derived classes.
Why not use std::map with an std::string and a pointer to the object?
You can use virtual member functions then. No need for pointers to
members.
#include <string>
#include <map>
#include <iostream>
class base
{
public:
virtual void do_something() const = 0;
virtual ~base() {};
};
class derived1 : public base
{
public:
virtual void do_something() const;
};
class derived2 : public base
{
public:
virtual void do_something() const;
};
void derived1::do_so mething() const
{
std::cout << "derived 1\n";
}
void derived2::do_so mething() const
{
std::cout << "derived 2\n";
}
int main()
{
typedef std::map<std::s tring, base*> objmap;
objmap mymap;
mymap.insert(st d::make_pair("f oo", new derived1()));
mymap.insert(st d::make_pair("b ar", new derived2()));
std::string s;
std::cout << "Give a string: ";
std::getline(st d::cin, s);
objmap::iterato r it = mymap.find(s);
if (it != mymap.end())
it->second->do_something() ;
else
std::cout << "no operation defined for that string\n";
for (it = mymap.begin(); it != mymap.end(); ++it)
delete it->second;
}
"Woodster" <mi****@127.0.0 .1> wrote in message
news:MP******** *************** *@news.westnet. com.au... I was trying to do something similar where I have several derived classes each containing it's own specific member variables.
I am trying to construct a report consisting of values extracted from my derived classes, where the report is entered as XML and I just extract custom tags and replace them with a value. The way I wanted to do this was to construct a list of name-value pairs, where the name is the tag I am searching for and the value is a pointer to a method from one of the derived classes. I do not know which classes the values will be coming from until I reach the desired tag.
How would/should I declare a structure that will simply store a string value (char *) and a function * as required so I couold call a specific class method based on one of these string values?
A pointer-to-member is specific to a single class (the class named in the
pointer's declaration), so you can't have a Base::* pointing to a member of
Derived. However, if all value member functions are declared virtual in
Base, you could do everything with pointers-to-members of Base.
Otherwise, you could associate each name with a proxy object or function,
and the proxy can call the appropriate member function of the appropriate
class of object. For example, the hierarchy proposed in Rolf Magnus's reply
could be used as a proxy hierarchy if it's not suitable as the value
hierarchy itself. Each class's override would have a hard-wired call to a
specific member function of a member object of the appropriate derived
class. That's more trouble than I'd like to go to if I could avoid it,
though.
A cruder, un-OO, approach is to associate each name with an enum. A switch
on the enum would then call the appropriate member function. This would get
messy if an object pointer needed to be kept along with the enum, because
you'd have to cast the pointer to the correct derived-class pointer in the
switch.
DW
xsist10 wrote:
I'm going to assume you mean a pointer to a function, since the "method"
keyword is not something used in C++ very often. whats the best method. delphi lets you do something very simple
I can't really think of a decent reason to want to do this anyway, more than
that I'm not sure its supported in C++, one thing that has been added to
C++ (not sure how long ago) is templates, these can be used to define
different values as part of functions and classes.
ptrMethodFoo : procedure
how do you do this in C++?
Regardless of this fact I did a search and came up with this following piece
of code, I've got to be honest and say that I didn't write this, it can be
found at the following address
krtkg1.rug.ac.b e/~colle/C/function_pointe rs.html:
typedef struct {
char *name;
int (*function)(cha r *, char *);
} command_type;
int do_command1(cha r *, char *);
int do_command2(cha r *, char *);
command_type command_list[]={
{"command1", do_command1 },
{"command2", do_command2 },
{NULL, NULL } /*we end the list with a NULL*/
};
main()
{
while (1)
{
command_type *command;
char buf[BUFSIZ];
if (NULL == gets(buf))
break; /*end of input*/
/*check if the command is in the list*/
for(command=com mand_list; command->name != NULL; command++)
if (!strcmp(comman d->name, buf))
/*yes it is -- execute associated code*/
command->function("argu ment1", "argument2" );
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: lawrence |
last post by:
I had some code that worked fine for several weeks, and then yesterday
it stopped working. I'm not sure what I did. Nor can I make out why it
isn't working. I'm running a query that should return 3 items from the
database. I then get a count of the return, which correctly tells me
that I've got 3 items. I then go into a for loop which I expect to
loop 3 times and print out the 3 items. Here is where things get
strange: it loops 3 times and...
|
by: lawrence |
last post by:
I posted before, but have now narrowed my problem down to this method.
At the start of the method, I test to make sure that I have a
resource, a pointer to data returned from a database. This test is
coming back true, so the next line runs, which attempts to get the
next row from the dataset. This brings back nothing. On the queries
I'm running right now, the first row will be fetched, but then no
further rows. If I expect 20 rows back, I...
|
by: Abhijit Deshpande |
last post by:
Is there any elegant way to acheive following:
class Base {
public:
Base() {}
virtual ~Base() {}
virtual void Method() { cout << "Base::Method called"; return;
}
};
|
by: Simon |
last post by:
I'm a js newbie trying to use some very simple js to call an ActiveX
object's methods.
I need to use a pointer to call an embedded ActiveX object's method to
receive a number.
As I understand it, js is typeless so how can I get a variable to be a
pointer type?
Passing var i as the parameter gets an undefined value in return.
Thanks IA, Simon
|
by: Nick Bishop |
last post by:
I have a problem where I call a method in a C++ class with a pointer
which is a static member in that class. When I use a debugger, I see
the pointer having a certain value, but when I step into the method
(F11), the pointer has a different value and is not pointing at
anything valid.
The small program (complete source below) demonstrates what I am
trying to achieve. The small program works perfectly, but when I
incorporate this logic...
| |
by: Rennie deGraaf |
last post by:
Let's say that I want to read a method pointer in from a stream. (I'm
not saying that this is a good design idea, or that I actually have a
reason to do this.) If I wanted to read in a function pointer, I could
do something like this:
#include <iostream>
int*(*readFunction())(int, int*)
{
unsigned long x;
|
by: Marcelo |
last post by:
Hi everybody,
I don't understand why I am having a problem in this code.
The problem is that my pointer *phist in main method, it is declared.
Then I send the pointer to my method, and this method creates a new object (a
Matrix) for it.
I suppose that after the new operator, my pointer is pointing to an object, so
when the method has finished, the very first pointer is still poitint to the
created method; however this is not working,...
|
by: nsharma78 |
last post by:
Hi,
I have a code as follows:
class A
{
public:
void print(){cout << "Magic" << endl;}
};
|
by: Erik de Castro Lopo |
last post by:
Hi all,
The GNU C compiler allows a void pointer to be incremented and
the behaviour is equivalent to incrementing a char pointer.
Is this legal C99 or is this a GNU C extention?
Thanks in advance.
Erik
|
by: Alex Vinokur |
last post by:
Does it have to be? :
sizeof (size_t) >= sizeof (pointer)
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |