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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:52:11+00:00 2026-06-03T04:52:11+00:00

Okay, so let’s say I have some random abstract base class Base and I

  • 0

Okay, so let’s say I have some random abstract base class Base and I have a class Foo which contains a pointer to this base class as a datamember. So

class Foo
{
    public:
        Foo(Base*);

    private:
        Base* ptr;
}

Now the reason I use a pointer to the base class is because I want to be able to choose which derived class my Foo object has a pointer to. Now the tricky part is, to me, the implementation of the constructor for Foo.

If I do it like this

Foo::Foo(Base* _ptr)
{
    Foo::ptr = _ptr;
};

it would be possible for the user to adjust the object pointed to by Foo::ptr since _ptr will still exist after the constructor has finished. I cannot make the object pointed to by _ptr constant because Foo::ptr needs to update itself regularly. Now I was thinking about adding a line _ptr = NULL at the end of the constructor, but that could be dangerous as well, since the user could try to dereference _ptr.

The only way I can think of to make this work is to make a copy of the object pointed to by _ptr and initializing Foo::ptr to the address of that copy. But then the object pointed to by _ptr would need to have a member function Clone() or something similar because I can’t call the copy constructor for an object of which I don’t know the class at compile-time.

So is there any elegant way of doing this if there is no Clone()? Or is that really the only possibility?

  • 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-03T04:52:12+00:00Added an answer on June 3, 2026 at 4:52 am

    Construct from a unique_ptr. That way your user has no access to the pointer after it has been used in the creation of a Foo object (unless he really wants to and uses get).

    e.g.

    #include <memory>
    struct Bar {};
    
    struct Foo {
      Foo(std::unique_ptr<Bar>&& ptr) : ptr_(std::move(ptr)) {}
      std::unique_ptr<Bar> ptr_;
    };
    
    int main()
    {
      std::unique_ptr<Bar> tt(new Bar());
      Foo f(std::move(tt));
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, let's say you have something like this: <span class=image style=background-image: url('http://www.example.com/images/image1.png')></span> Every CSS
Okay, let's say we have a class defined like public class TestClass { private
Okay, so let's say I have a class file called Orange, and then two
Okay, let's say that I have b.exe, which takes a string argument. I want
Okay let me see if I can explain this right. In wordpress we have
Let's say I have an NSSecureTextField in my app. Is it okay for me
Okay let me do a better job explaining this. Assume I have a person
Okay, so let's say I have a mysql database table with two columns, one
Okay so let's say I have a folder and in a folder I have
I have this value from database: '2009-1-1 00:00:00', okay, let me paste my code:

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.