2

I am type casting a class to use storage set by another class. I want to invoke the constructor of the first class when type casting is performed. Is there any way to achieve this?

Consider the following code:

class A{
      int x;
public:
      A() {x = 0;}
};

class B{
      double x;
public:
      B() {x =0.0f;}
};

B * bObj = new();
A * aObj = reinterpret_cast < A * > ( bObj );     //Just for the sake of explaining

Now aObj references to the memory allocated to bObj & uses it & stores its contents there. I am trying to get the constructor of A class called when the typecasting is performed or after. Please help.

I want to use aObj and the constructor for A to be called. which doesn't happen. aObj is, no doubt ,getting memory allocated however, the constructor call doesn't happen. And it has garbage values filled in it.

JNSVS
  • 23
  • 3
  • 2
    Could you describe the *actual problem* you are trying to solve with this? – NPE Mar 01 '14 at 14:20
  • "And it has garbage values filled in it." If course it does, because you're invoking UB by the way you're using `reinterpret_cast`. Please explain why you think you need to do this... – Dark Falcon Mar 01 '14 at 14:27
  • @NPE: I am learning C++ and trying to store objects in a universal buffer. (bObj acting as buffer in above snippet). aObj does have memory access but constructor call doesn't happen which Is what i m trying to achieve. – JNSVS Mar 01 '14 at 14:30
  • A cast operator doesn't call the constructor of the target type. Your assumptions are wrong. – πάντα ῥεῖ Mar 01 '14 at 14:30
  • @πάντα ῥεῖ : Agreed. I am trying to get the constructor call done somehow. Is there any way? – JNSVS Mar 01 '14 at 14:31

2 Answers2

1

Constructing an A object in the storage of a B object is generally Undefined Behavior.

You can do it (with a placement new expression) when type B is an array of char, not a class with a constructor, but it's extremely rarely needed, only for experts, and it requires manual invocation of the A object's destructor.

If you are trying to just save memory then you can

  • Ignore the memory usage.
    With today's computers that's generally the best solution.

  • Use derived classes and virtual member functions.

  • In the worst case, use a tagged union.
    "Tagged" means that there is a type identification value, a type "tag".

If you are trying to save execution time for PIMPL idiom,

  • Measure. Probably avoiding a dynamic allocation and indirection isn't really so critical.

If something else, please state what.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • placement new works well for me as suggested originally by NPE. Thank you for your input as well. – JNSVS Mar 01 '14 at 15:36
  • @user3368334: it may appear to "work" but it's **undefined behavior**. that means that you'll get a visit from Mr. Murphy at the most inconvenient time. – Cheers and hth. - Alf Mar 01 '14 at 15:37
0

I am learning C++ and trying to store objects in a universal buffer. (bObj acting as buffer in above snippet). aObj does have memory access but constructor call doesn't happen which Is what i m trying to achieve.

It sounds like you need to read up on placement new. See What uses are there for "placement new"? and What is "placement new" and why would I use it? for some background and examples of use.

Bear in mind that using placement new correctly requires care. It is very easy to get it wrong and end up with undefined behaviour.

In particular, you'll need to rethink how you allocate the buffer.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @Cheersandhth.-Alf: I find your comment misleading. Placement `new` is probably the best tool for the stated problem ("store objects in a universal buffer"). I don't think it is our place to judge how qualified the OP is and decide for them which tools they should be allowed to use. – NPE Mar 01 '14 at 15:09
  • your response comment is actively misleading. The stated problem is **not** to "store objects in a universal buffer". – Cheers and hth. - Alf Mar 01 '14 at 15:40
  • @NPE tx for the help ! I really appreciate it. – JNSVS Mar 01 '14 at 16:04