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

  • Home
  • SEARCH
  • 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 9188203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:56:57+00:00 2026-06-17T19:56:57+00:00

I have some class structure for the project I build for my company. At

  • 0

I have some class structure for the project I build for my company. At some point, I’ve seen a “usually unwanted” slicing of derived object could actually make my code efficient. Please observe:

class Base {
  private:
    int myType; //some constant here
  public:
    Base(): myType(1)
    { 
       if(someTest()) 
       { 
         myTest = 0; // signifies that this object is not working well for me
       }  // if the test succeeds, now I know this is a TYPE1 object
    }

    virtual bool someTest() { /* does make some validation tests */ } 
    int GetMyType() { return myType; }

    // some more virtual functions follow
}

class Derived: public Base {
  public:
    Derived() : Base() 
    { 
      if( someTest() /*calls the derived*/ ) 
      { 
        myType =2; // signifies that this is a TYPE2 object
        /* Do something more*/ 
      }  
    }

    virtual bool someTest() { /* does make OTHER validation tests */ } 
}

Now, if someTest() at Derived fails, this shows that my object is actually of Base type (myType = 1) or an faulty object (myType =0). Since these stuff are at constructors, and since I cannot use exception handling (embedded system); I thought of this:

{ 
  // this is the object holder test block
  // there is some latch - Base* - above = let's call it "ObjectHolder"
  extern Base* ObjectHolder;


  Derived * deriv = new Derived();
  int dType = deriv->GetMyType() ;
  if( dType == 1) // if test at base succeded but the one in derived failed
     Base = & static_cast<Base> (*deriv); // specifically slice the object
  else if(dType =2) 
     Base = deriv;
  else
     delete deriv; // this is a TYPE0 - nonworking- object so don't latch it

  // use other implemented methods to do work ...

}

Now, why have I got such a design? Well, while I was designing the classes, since the methods of inner workings of “Base” (type 1) and “Derived” (type 2) classes differ (and there is a possibilty of type 3,4,5…. objects that are also different in inside) and since I didn’t want to make and “if-then-else” check in every single method; I thought I’d make such a design and make the differing methods virtual so they could be called correctly (with thanks to polymorphism) whilst the common methods can be at some Base class.

But now, firstly, I’m not sure if that weird syntax ( & static_cast < Base > *deriv ) is correct (tested and seemed like working but I just want to make sure this is not because of luck); and secondly, I’m not sure if this is the “suggested method” to achive this – I kinda suspect that this syntax can cause memory leaks or something…

Changed code a little bit (also corrected syntax) to make the problem clearer.

Now, since the &(static_cast(*deriv)) is a faulty approach, I was thinking if I can make a “copy constructor” which bypasses the check at the Base class (which is actually the reason I try these stuff – I don’t want that test to be run in some cases). Like below:

class Base { 
    // other stuff is like above

    // cc:
    Base(const Base &other) : myType(other.myType)
    {
         // no test is performed here!
    }

    // like other one above
}

With this written, I think I can do that now at the test block:

{ 
   // the code above

   if( dType == 1) // if test at base succeded but the one in derived failed
     Base = new Base(*deriv); // specifically slice the object with cc

    // code goes on ... 
}

What about this?

  • 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-17T19:56:59+00:00Added an answer on June 17, 2026 at 7:56 pm

    Surely, if you want to create two types of objects, the correct way is to have a factory function that contains the “if (sometest)” and then creates the correct object type:

     Base* factory(...) 
     {
         if(sometest())
         {
             return new Derived;
         }
         else
         {
              return new Base;
         }
     }
    

    That way, you don’t unnecessarily create the derived type of object when you actually want a Base type object. Bear in mind also that if you slice the object, your vtable
    associated with Derived objects that have been sliced will still be a Derived vtable.

    Edit: Here’s some code to show the problem with slicing:

    #include <iostream>
    
    using namespace std;
    
    class Base
    {
    public:
        virtual void func() { cout << "Base" << endl; }
    };
    
    
    class Derived : public Base
    {
    public:
        virtual void func() { cout << "Derived" << endl; }
    };
    
    
    int main()
    {
        Base *list[10];
        for(int i = 0; i < 10; i++)
        {
            if (i % 2)
            {
                list[i] = new Base;
            }
            else
            {
                list[i] = new Derived;
            }
        }
    
        list[2] = &*(static_cast<Base*>(list[2]));
    
        for(int i = 0; i < 10; i++)
        {
            list[i]->func();
        }
        return 0;
    }
    

    Output:

    Derived
    Base
    Derived     <- should have changed to "Base" if questio was "right".
    Base
    Derived
    Base
    Derived
    Base
    Derived
    Base
    

    Using the syntax for “slicing” as given in the original question:
    &(static_cast<Base>(*list[2])); (as per adapted to the code posted here), givesn an error in both clang++ and g++ compiler. The essence of the message is the same. This is clang’s variant:

    error: taking the address of a temporary object of type 'Base'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have next package structure webService.ear -- dependency.jar -- some.class Can I remove some.class
Say you have a simple class with some storage data structure (list, vector, queue,
Basically I have some class objects, each with three properties. Once one class object
I have some JS you'll see below. I want the inner class object to
I have some trouble getting a good folder structure in my project and i
I have some class that I'm passing as a result of a service method,
I have some class SimpleButton . It is a Child of Button class. Button
Suppose I have some class which has a property actor_ of type Actor .
So I have some class starting with #include <wchar.h> #include <stdlib.h> and there is
the basic idea is that you have some class that has a reference type

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.