Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:30:49+00:00 2026-05-28T21:30:49+00:00

In C++, assume following class hierarchy: class BaseClass { }; class ChildClass : public

  • 0

In C++, assume following class hierarchy:

class BaseClass { };
class ChildClass : public BaseClass { };

Further assume factory classes for these two classes with a common, templated base class:

template<typename T>
class Factory {
public:
  virtual T* create() = 0;
};

class BaseClassFactory : public Factory<BaseClass> {
public:
  virtual BaseClass* create() {
    return new BaseClass(&m_field);
  }
private:
  SomeClass m_field;
};

class ChildClassFactory : public Factory<ChildClass> {
public:
  virtual ChildClass* create() {
    return new ChildClass(&m_field);
  }
private:
  SomeOtherClass m_field; // Different class than SomeClass
};

Note that the size/internal structure of ChildClassFactory and BaseClassFactory is different due to their different fields.

Now, if a have an instance of ChildClassFactory (or Factory<ChildClass>), can I safely cast it to Factory<BaseClass> (via reinterpret_cast)?

Factory<ChildClass>* childFactory = new ChildClassFactory();

// static_cast doesn't work - need to use reinterpret_cast
Factory<BaseClass>* baseFactory = reinterpret_cast<Factory<BaseClass>*>(childFactory);

// Does this work correctly? (i.e. is "cls" of type "ChildClass"?)
BaseClass* cls = baseFactory->create();

I know that you can’t always cast templated classes this way, but in this special case a cast should be safe, shouldn’t it?

I’ve tested it with Visual C++ 2010 and it does work. My question now is whether this is portable to other compilers?

Update: Since there has been some confusion let me clarify some more what’s (supposed to be) important in my example:

  • ChildClass is a child class of BaseClass
  • A user of Factory<BaseClass> doesn’t know what child class of BaseClass will be created. All he knows is that BaseClass is created.
  • Factory<T> has no fields of its own (other than the vtable).
  • Factory::create() is virtual
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:30:50+00:00Added an answer on May 28, 2026 at 9:30 pm

    No, it is not. You may not use the result of a reinterpret_cast other than to cast stuff back, except for a few special cases:

    ISO14882:2011(e) 5.2.10-7:

    An object pointer can be explicitly converted to an object pointer of
    a different type.70 When a prvalue v of type “pointer to T1” is
    converted to the type “pointer to cv T2”, the result is static_cast(static_cast(v)) if both T1 and T2 are standard-layout
    types (3.9) and the alignment requirements of T2 are no stricter than
    those of T1, or if either type is void. Converting a prvalue of type
    “pointer to T1” to the type “pointer to T2” (where T1 and T2 are
    object types and where the alignment requirements of T2 are no
    stricter than those of T1) and back to its original type yields the
    original pointer value. The result of any other such pointer
    conversion is unspecified.

    To make a possible failure scenario more clear, consider multiple inheritance, where using a static_cast or dynamic_cast would sometimes adjust the pointer value, but reinterpret_cast will not. Consider casting in this example from A* to B*:

    struct A { int x; };
    struct B { int y; };
    struct C : A, B { };
    

    To understand how your code fails in a different way too, consider how most compilers implement the virtual function call mechanism: With virtual pointers. Your instance of ChildClassFactory will have a virtual pointer, pointing to the virtual table of ChildClassFactory. Now when you reinterpret_cast this beast, it just happens to “work” incidentally, because the compiler expects some virtual pointer, pointing to a virtual table that will have the same/similar layout. But it will still contain the values pointing to the ChildCLassFactory virtual functions, thus these functions would be called. All of this is long after invoking undefined behaviour. It is as if you are jumping with a car into a large canyon and thinking “hey, everything is driving fine” just because you have not hit the ground yet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets assume following classes definition: public class A { public final static String SOME_VALUE;
Assume I have the following class-hierarchy in C++: class AbstractBaseClass { public: // Note:
assume the following class is given: class Base{ public: Base() {} Base( const Base&
Assume the following entity classes: public class Player { public virtual int ID {
Assume the following class: public class MyEnum: IEnumerator { private List<SomeObject> _myList = new
Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public
Assume I have the following classes class Genre { static hasMany=[author:Author] } class Author{
Assume the following type definitions: public interface IFoo<T> : IBar<T> {} public class Foo<T>
Let's assume I have the following code: public class MainClass { public static void
Assume following: public class MyFunkyTable : DbObject { // this class will be generated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.