Address of constructor

K

Ken Human

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?
 
A

abecedarian

Ken said:
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??
 
R

Rolf Magnus

Ken said:
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.
 
K

Ken Human

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.
 
P

Phlip

Ken said:
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.
 
D

David White

Ken Human said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,293
Messages
2,571,506
Members
48,193
Latest member
DannyRober

Latest Threads

Top