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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:40:31+00:00 2026-05-14T06:40:31+00:00

I have a class Model: class Model { … boost::shared_ptr<Deck> _deck; boost::shared_ptr<CardStack> _stack[22]; };

  • 0

I have a class Model:

class Model
{
    ...

    boost::shared_ptr<Deck>  _deck;
    boost::shared_ptr<CardStack> _stack[22];
};

Deck inherits from CardStack.

I tried to make _stack[0] point to the same thing that _deck points to by going:

{
    _deck = boost::shared_ptr<Deck>(new Deck());
    _stack[0] = _deck;
}

It seems that the assignment to _deck of _stack[0] results in a copy of _deck being made. (I know this because modifications to _stack[0] do not result in modifications to _deck.) How can I get them to point to the same thing?

Ok – no copy constructor is being called. I have verified this by implementing it and seeing if it gets called – it doesn’t.

However – I have a function that operates on CardStack objects:

void TransferSingleCard(CardStack & src, CardStack & dst, Face f)
{
    if( !src._cards.empty() )
    {
        src._cards.back().SetFace(f);
        dst.PushCard(src._cards.back());
        src._cards.pop_back();
    }   

}

Now – when I call:

{
    TransferSingleCard(*_stack[DECK], _someotherplace, FACEDOWN);
    std::cout << *_stack[DECK];
    std::cout << *_deck;
}

I get this output (where std::cout on a CardStack will print out the size of that stack):

Num(103) TOP
Num(104) TOP

… so I’ve concluded (incorrectly?) that _stack[DECK] points to something different.

The Deck

class Deck : public CardStack
{
public:
    Deck(int numsuits=2, StackIndex index = NO_SUCH_STACK );
    Deck::Deck( const Deck & d);
    int DealsLeft() const;
    void RecalcDealsLeft();
private:
    int _dealsleft;
};
  • 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-14T06:40:31+00:00Added an answer on May 14, 2026 at 6:40 am

    This example – derives from @Neil’s answer, tries to emulate what you say is happening. Could you check that it works as expected (A and B have the same count) on your system.

    Then we could try and modify this code or your code until they match.

    #include <boost/shared_ptr.hpp>
    #include <iostream>
    
    class A {
        public:
        virtual ~A()
        {
            std::cerr << "Delete A" << std::endl;
        }
        int _count;
        void decrement()
        {
            _count --;
        }
    };
    class B : public A {
        public:
        virtual ~B()
        {
            std::cerr << "Delete B" << std::endl;
        }
    };
    
    int main()
    {
        boost::shared_ptr<B> b(new B);
        b->_count = 104;
        boost::shared_ptr<A> a;
        a = b;
    
        a->decrement();
    
        std::cerr << "A:" << a->_count << std::endl;
        std::cerr << "B:" << b->_count << std::endl;
        return 0;
    }
    

    EDIT:

    So from the comment, we know the original pointers are correct, so now we need to trace.

    Either:

    1. log pointers to see when they change.
    2. Use watchpoints in a debugger to see when the pointer changes.
    3. Use a third shared pointer to see which pointer is changed.
    4. Introduce a function that changes both pointers at the same time.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class CardStack . I have several classes that inherit from CardStack
I have a class which derives from a QWidget and a model class. Based
I have a class Similar to this public class Model { public TimeSpan Time1
I have a class I model bind and I want to use output caching
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
In a client application I have a DBIx::Class model 'Todo' that can be linked
I have a model class similar to the following: using System; using System.Collections.Generic; using
In my model I have: class Log < ActiveRecord::Base serialize :data ... def self.recover(table_name,
I have a model: class Example(models.Model): unique_hash = models.CharField(max_length=32,unique=True) content = models.FileField(upload_to='source',blank=True,verbose_name=HTML Content File)
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content

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.