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 8158835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:44:28+00:00 2026-06-06T17:44:28+00:00

I have an abstract class Engine3D and it’s structures ( Vector3D , etc.) in

  • 0

I have an abstract class Engine3D and it’s structures (Vector3D, etc.) in the header files.

Now, I have an implementation for that class, ConcreteEngine3D : Engine3D. Also, I have other classes like ConcreteVector3D : Vector3D that have additional members and methods that will be used inside ConcreteEngine3D (for the sake of this example, let’s say it’s float length and calculateLength()).

In main.cpp I have the code:

#include "ConcreteEngine3D.h"
Engine3D * engine;
...
int main(){
    engine = new ConcreteEngine3D();
    Vector3D* vector = new Vector3D(engine, 10, 5, 2);
}

I want the variable vector be of type ConcreteVector3D*.

I will need that type in ConcreteEngine3D, but in main.cpp I shouldn’t even know it’s that type and don’t have to use extended fields like length. Also, I can’t use in main.cpp anything specifically from ConcreteEngine3D.h (only Engine3D.h) – it’s for the flexibly, changing the implementation must means only changing the inclusion and line with new ConcreteEngine3D().

I do not want to modify that code above or original headers from Engine3D.

In the constructor of Vector3D I always put a pointer to Engine3D object (and I give here ConcreteEngine3D type).

Maybe can I do something in the constructor of Vector3D to change the type of it?

For example call Vector3D* Engine3D::convert(Vector3D v) inside of the constructor which will be inherited from Engine3D in ConcreteEngine3D (which creates a new ConcreteVector3D object with fields from Vector3D and returns it).

Of course that code doesn’t work:

Vector3D::Vector3D(Engine3D *engine){
    //how to 'return' or 'convert' the type to the one that returns engine->convert(this);
}

So basically, I want to get the effect of code below but without the line vector = engine->convert(vector) or Vector3D* vector2 = new ConcreteVector3D(engine, 10, 5, 2).

#include "ConcreteEngine3D.h"
Engine3D * engine;
...
int main(){
    engine = new ConcreteEngine3D();
    Vector3D* vector = new Vector3D(engine, 10, 5, 2); //produces Vector3D, not ConcreteVector3D
    vector = engine->convert(vector); //cannot be here! but creates the right object
    Vector3D* vector2 = new ConcreteVector3D(engine, 10, 5, 2); //also creates rights object, but cannot be here!
}

Also, I don’t want to use factory in Engine3D or ConcreteEngine3D. I want to allow the ‘user’ create Vector3D just the way I have written in the first code.

  • 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-06-06T17:44:29+00:00Added an answer on June 6, 2026 at 5:44 pm

    It sounds like you would want to use abstract factory pattern to instantiate your objects so that you would not have to directly call constructors of the concrete classes. Then, in case you would want to change the concrete types that implement your interfaces, you would only need to either link in a different implementation of your abstracy factory, or select a proper factory implementation at run time.

    Edit: Missed the “don’t want to use factory”… Anyway you will need some kind of redirection, because constructor will return the type you instantiate. Closest you can get is probably creating a static factory method “create” to class Vector3D that returns a pointer to Vector3D, but internally creates an instance of concrete implementation class. In case you do that, it is good practice to make the constructor of Vector3D private to prevent creating the vector in “wrong way”.

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

Sidebar

Related Questions

I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class that has a virtual method. The method is virtual
I have an abstract class and two sub classes that extend it. I have
I have an abstract class that is inherited by a number of other classes.
I have an abstract class called Node . It contains a constructor that takes
I have an abstract class, AbsClass that implements an interface, IClass . IClass has
I have one abstract class named contact and another class called client that inherits
I have an abstract class Step, and many descendant Step classes that I would
I have one main interface and an abstract class implementing all derivable methods (that
I have an abstract class that other classes are inheriting this. public abstract class

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.