Link to home
Start Free TrialLog in
Avatar of pratish
pratish

asked on

Address of a constructor

How can i obtain the address of a constructor function for a c++ class object?
I am able to obtain addresses of other member functions of an object but not of a constructor.
Avatar of KangaRoo
KangaRoo

You can't call the constructor (directly) either.
As kangroo said, you many not call a constructor directly.  For that reason, you may not take the address of the constructor prodedure as you could use this pointer to call the function, which you are not allowed to do.


You can use placement new to acheive the same results however.
If you just need the function pointer in order to construct the object then maybe you can try making a factory method and taking the address of that:

class Foo {
public:
   Foo(void);
   Foo(const Foo& f);
   ~Foo(void);

  static Foo createFoo(void) {
    return Foo();
  }
  statuc Foo* createNewFoo(void) {
    return new Foo();
  }
};

If you need the address for another reason then you're probably out of luck.
ASKER CERTIFIED SOLUTION
Avatar of abhayap20
abhayap20

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Indeed, bye  ;)
>> so u don't have to worry about
>> calling of the constructo
But the question is concerned with getting the address of the constructor not the need for calling it.  

>> it can be accesed by THIS (special type)
>> of pointer. so by knowing value of THIS
>> pointer u can get the address
How?  ... You can't
Avatar of pratish

ASKER

The thing is, if the constructor is a function, it has to have an address in memory. So how do i get that address in memory where the constructor func sits?
- Thanks
>> The thing is, if the constructor is a function,
>> it has to have an address in memory.
That is in fact one reason why you can't.  It may not exist as a standard function.  There are many optimizations possible that may make the constructor code different than a standard function.  The most obvious is that constructors may be inlined and therefore might not exist as a true function.  This will tend to be true of constructors for small classes.  However constructors for complex classes might not be inlined, you will still be non-standard.  They may mot set up stack frames for example.  This allows the code for derived class constructors to be "strung together" with code for the base class constructor.  By jumping into the middle of the code, at the right place the compiler can construct a particular derived class or a base class.  The is code is not safe to call from C++ because it does not follow C++ calling conventions.   Another optimization may be to use registers to pass information about the class so that "standard" functions can quickly initialize it.  like registers might be used to speicfy the location and size of the object so that it can quickly be initialized to 0s before other initialization might occur. Again, this can't be called from standard C++.  

If you were to explain what it is you want to do, there might be another way.  It is very likely that placement new will work for you, but I can't say for sure.
hey,
i have written that u can get the address of the constructor by knowing the value of THIS pointer.

bye,
abhay.
You have written that, but it is untrue.  If it is true, show us an example.
What is abhayap20 talking about?  There is no way to directly call the constructor using the constructor name of the constructor OR a pointer (which cannot be obtained) anyway.  The only way to call it is to construct an object.  If his compiler allows this then it's a bug.  Some compilers allow you to directly call the destructor (which is bad thing to do, but I'm not sure what the standard actually says about this).

The following code SHOULD fail to compile:
//==============================================================================
#include <iostream.h>

class Foo
{
public:
  Foo(int x = 0);
  Foo(const Foo& f);
  virtual ~Foo(void);
  Foo& operator=(const Foo& f);
  int get(void) const;
 
private:
  int x_;
};

Foo::Foo(int x) : x_(x) { }

Foo::Foo(const Foo& f) : x_(f.x_) { }

Foo::~Foo(void) { }

Foo& Foo::operator=(const Foo& f)
{
  if (this==&f) return(*this);
  this->Foo(f.x_); // this is illegal!!!  should NOT compile
  return (*this);
}

int Foo::get(void) const { return x_; }

int main()
{
  Foo f1(5);
  Foo f2(10);

  cout << "f1=" << f1.get() << ", f2=" << f2.get() << endl;
  f1.operator=(f2);
 
  cout << "f1=" << f1.get() << ", f2=" << f2.get() << endl;

  f1.Foo(5); // this is illegal!!!  should NOT compile
 
  cout << "f1=" << f1.get() << ", f2=" << f2.get() << endl;

  return 0;
}
//==============================================================================

What would really help here is knowing what pratish is trying to do with the address of the constructor, and then an alternative that could be developed.

1) Is he/she trying to reinitialize an obect after it has been constructed?  Then perhaps an initialize method which is called by the constructor is needed.  The address of that initialize method can definitely be taken.

2) Is he/she trying to construct new objects?  Then a factory method is neded and the address of that could be taken.

3) Is the constructor address being used in a conditional statement or as some kind of identifier in order to control program execution?  In that case then we need to devise a way to identify the class/object in another matter.  

I hope this helps.  If abhayap20 could post an example of what is meant by the "value of THIS pointer" giving the address of the constructor then maybe we could get a little further on this.  Currently, I have no idea what is meant by "THIS", from what I know "THIS" is  not even a keyword when it's in upper-case.

>> Some compilers allow you to directly call the destructor
>> (which is bad thing to do, but I'm not sure what the standard
>>actually says about this).
You need to call the destructor 'manually' for allocations with placement new.
Right.  it would have been nice if the destructor could also not be called directly and instead the code would have to call a placement delete function instead.  Unfortunetely, that design doesn't work because the "placement delete" function has the same parameters as the regular delete function, i.e. it is not a seperate function from the regular delete.  So instead the destructor must be called to "balance" the call to placement new.  Sort of messy.
pratish, you accepted an incorrect answer!