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.
pratishAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KangaRooCommented:
You can't call the constructor (directly) either.
0
nietodCommented:
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.
0
barrycCommented:
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.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

abhayap20Commented:
hey,

constructor is called as soon as object is created. so u don't have to worry about calling of the constructor.

also  it can be accesed by THIS (special type) of pointer.
so by knowing value of THIS pointer u can get the address

bye,
abhay
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
Indeed, bye  ;)
0
nietodCommented:
>> 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
0
pratishAuthor Commented:
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
0
nietodCommented:
>> 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.
0
abhayap20Commented:
hey,
i have written that u can get the address of the constructor by knowing the value of THIS pointer.

bye,
abhay.
0
nietodCommented:
You have written that, but it is untrue.  If it is true, show us an example.
0
barrycCommented:
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.

0
KangaRooCommented:
>> 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.
0
nietodCommented:
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.
0
nietodCommented:
pratish, you accepted an incorrect answer!  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.