?
Solved

Address of a constructor

Posted on 1999-10-29
14
Medium Priority
?
801 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:pratish
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2169057
You can't call the constructor (directly) either.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2169248
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
 

Expert Comment

by:barryc
ID: 2169433
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Accepted Solution

by:
abhayap20 earned 20 total points
ID: 2169677
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2169710
Indeed, bye  ;)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2169829
>> 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
 

Author Comment

by:pratish
ID: 2172267
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
 
LVL 22

Expert Comment

by:nietod
ID: 2172396
>> 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
 

Expert Comment

by:abhayap20
ID: 2172579
hey,
i have written that u can get the address of the constructor by knowing the value of THIS pointer.

bye,
abhay.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2172651
You have written that, but it is untrue.  If it is true, show us an example.
0
 

Expert Comment

by:barryc
ID: 2174676
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2174725
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 2174960
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
 
LVL 22

Expert Comment

by:nietod
ID: 2196592
pratish, you accepted an incorrect answer!  
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question