473,385 Members | 1,620 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,385 software developers and data experts.

Address of constructor

I need to create a pointer to a class's constructor and I'm not sure how
it can be done, if at all. My code follows:

class A {
public:
A() { };
void B() { };
};

A (A::* pA)(void) = &A::A;
void (A::* pB)(void) = &A::B;

int main() {
return 0;
}

The line A (A::* pA)(void) = &A::A; gives me a compiler error: cannot
take address of this member function.

Is there any way around this?
Jul 23 '05 #1
7 5270
Ken Human wrote:
I need to create a pointer to a class's constructor and I'm not sure how it can be done, if at all.


The answers in comp.lang.c++.moderated have not been good enough??

Jul 23 '05 #2
ab*********@spambob.com wrote:
Ken Human wrote:
I need to create a pointer to a class's constructor and I'm not sure


how
it can be done, if at all.

The answers in comp.lang.c++.moderated have not been good enough??


I'm not subscribed to that newgroup.
Jul 23 '05 #3
Ken Human wrote:
I need to create a pointer to a class's constructor and I'm not sure how
it can be done, if at all.
It can't.
My code follows:

class A {
public:
A() { };
void B() { };
};

A (A::* pA)(void) = &A::A;
void (A::* pB)(void) = &A::B;

int main() {
return 0;
}

The line A (A::* pA)(void) = &A::A; gives me a compiler error: cannot
take address of this member function.
Why do you think you need something like that, or IOW: what are you trying
to do?
Is there any way around this?


There is no way to take the address of a constructor. It wouldn't gain you
much anyway, since the constructor is only one part of several steps
involved in creating an object.

Jul 23 '05 #4
In article <08********************@comcast.com>,
Ken Human <ke******@comcast.net> wrote:
I need to create a pointer to a class's constructor


Why? What will you do with this pointer?
--
Mark Ping
em****@soda.CSUA.Berkeley.EDU
Jul 23 '05 #5
Rolf Magnus wrote:
[...]


Why do you think you need something like that, or IOW: what are you trying
to do?

I'm injecting my code in to binary file at run time with the Microsoft
Detours library. This binary file has a number of classes of which I'd
like to create objects.

Suppose at address 0x0040703B there is a function CMyClass::CMyClass().
I have a class in my code called CDetourMyClass. I need
CDetourMyClass to call the function at 0x0040703B so that a
CDetourMyClass will be constructed in the same way as a CMyClass.

As for simply creating a void *ptrCMyClass__CMyClass() that points to
0x0040703B and calling that in the constructor of CDetourMyClass,
ptrCMyClass__CMyClass() returns the same address to my code as it will
the next time it (CMyClass::CMyClass()) is called by the binary.

I don't think I'm going about this the right way, and I'm open to
suggestions.
Jul 23 '05 #6
Ken Human wrote:
I need to create a pointer to a class's constructor and I'm not sure how
it can be done, if at all.


Someone may have pointed out the Prototype and Factory Patterns by now.

You can't take the pointer because a class's constructor and destructor are
"not functions", per the Standard. They are just magic blocks that bracket
an object's lifespan.

Of course your implementation will provide a function with an address for
them. "Not a function" means the compiler locks down certain shenanigans,
such as taking an address.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand


Jul 23 '05 #7
"Ken Human" <ke******@comcast.net> wrote in message
news:6v********************@comcast.com...
Rolf Magnus wrote:
[...]


Why do you think you need something like that, or IOW: what are you trying to do?
I'm injecting my code in to binary file at run time with the Microsoft
Detours library. This binary file has a number of classes of which I'd
like to create objects.

Suppose at address 0x0040703B there is a function CMyClass::CMyClass().
I have a class in my code called CDetourMyClass. I need
CDetourMyClass to call the function at 0x0040703B so that a
CDetourMyClass will be constructed in the same way as a CMyClass.


Even if it could be done, where would you get the piece of raw memory from
which the pointed-to constructor would construct an object, and, when
called, how would it know where that piece of memory is?
As for simply creating a void *ptrCMyClass__CMyClass() that points to
0x0040703B and calling that in the constructor of CDetourMyClass,
ptrCMyClass__CMyClass() returns the same address to my code as it will
the next time it (CMyClass::CMyClass()) is called by the binary.

I don't think I'm going about this the right way, and I'm open to
suggestions.


How about a pointer to an ordinary function that returns a pointer to a new
CMyClass object?

DW
Jul 23 '05 #8

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

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
8
by: Hal Vaughan | last post by:
I may have my terms mixed up (I'm not a professional programmer), but if I do this: var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour"); var array2 = new Array(); var test1...
2
by: Keith Langer | last post by:
I have a TCPListener which needs to listen on my LAN IP as well as the loopback address. In VS.Net 2002 the TCPListener constructor could accept the port as an argument and it automatically bound...
14
by: Dennis | last post by:
Hi I have a little problem as stated above (haven't found any solution on the www). I have a list of objects (particles) where I have a member of a template vector (array1d from TNT). I...
7
by: Nolan Martin | last post by:
is a static functions address constant? ie.. static void func(); write_to_file(&func); Restart program... static void func(); void (*funcPtr) ();
2
by: Ariel | last post by:
When passing an object by value to some function foo(), the copy constructor gets called and inside it i see that the address of the temporary obect is X. but when entering to foo() i see that the...
5
by: Horst Walter | last post by:
What is wrong here? IPAddress ipAddress = IPAddress.Parse("10.10.20.1"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port); this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE...
5
by: Gary Wessle | last post by:
Hi I am getting a warning when I run this program, I am interested to know why the compiler is warning. thank you **************** warning **************** $ make clean; make rm -rf *.o...
0
by: daniell | last post by:
/* Triangle.cpp */ // Use a pure virtual function. #include <iostream> #include <cstring> using namespace std;
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.